2016-04-12 13 views
0

私はString ArraListをDBから記入しています。 Object ArrayList各オブジェクトにはStringが含まれています。 の各オブジェクトの文字列の1つとString ArrayListを比較したいと思います。 Object ArrayListのオブジェクトと等しい場合は削除する必要があります。ここで は私Methodコードです:私はこの方法を使用する場合Object ArrayListとString ArrayListの比較

 public ArrayList checkMatches(ArrayList<IceCream> iceCreams, ArrayList<String> blackListPartCodes) { // output is filtered Object ArrayList and two Input, 
//one is the Object ArrayList and other is String ArrayList 
     int i, j; 
     for(i = 0; i<iceCreams.size(); i++) { // Object ArrayList 
      IceCream iceCream = iceCreams.get(i); 
      for(j = 0; j<blackListPartCodes.size(); j++) { //String ArrayList 
       if ((iceCream.getPartCode().matches(blackListPartCodes.get(j)) || iceCream.getPartCode().equals(blackListPartCodes.get(j)))) { 
        iceCreams.remove(iceCream); 
       } 
      } 
     } 
      return iceCreams; 
     } 

[OK]を、それは私のObjectからいくつかのオブジェクトを削除し、ArrayListののな長さを減少させますが、正しく動作しません。 私は何か間違っている?

私は、メソッドの作業罰金場合やない確認するために私のアプリでこのコードを使用:

  Toast.makeText(getApplicationContext(), "Before : " + iceCreams.size(), Toast.LENGTH_LONG).show(); //324 

    checkMatches(iceCreams, blackListPartCodes); 
    Toast.makeText(getApplicationContext(), "After : " + iceCreams.size(), Toast.LENGTH_LONG).show(); //200 

iceCreamsの最初のlenghtは324で、方法が相互作用するときlenghtは、私は文字列のArrayList(blackListPartCodesを読ん200 です)DBから、私がselect Count(myField) from MyTableを使用するとき、それは215行を持っていると言います。 それは正常に動作するかどうかを意味する324-215は109です。 反対側からは、各オブジェクトのストリングの1つがListViewに表示されます。

 for(int i = 0; i<iceCreams.size(); i++) { // the filtered iceCreams after calling `checkMatches`method. 
     typeArray.add(iceCreams.get(i).getPartName()); // String ArrayList 
     typeAdapter.getItemViewType(R.id.listView); //Adapter of the Array 
     iceCreamTypeList.setAdapter(typeAdapter); //Adapter set to ListView 
    } 

しかし、ビューにblackListPartCodesにあるフィールドがまだ存在:この経由 。

+0

'しかし、あなたが何を意味するcorrectly'動作しませんか? –

+0

私は推測させてください: 'ConcurrentModificationException'。 SOとウェブ全体で簡単に検索できます。また、一般的に、 'remove(Object)'を使うと、リストを反復処理する必要があるので、大したことはありません。あるいは、正しいものを削除していない場合は、 'equals'実装を示すことが有用かもしれません。 –

+0

私は質問を編集しました。 –

答えて

0

私はあなたの方法を簡潔にしようとしました。 これで配列をスローし、新しい配列リストにマッチを配置します。触れたり、既存のアレイを変更されていない:

public ArrayList checkMatches(ArrayList<IceCream> iceCreams, ArrayList<String> blackListPartCodes) { 
    ArrayList<IceCream> matches = new ArrayList(); 

    for (IceCream iceCream : iceCreams) { 
    for (String blackListPartCode : blackListPartCodes) { 
     if (blackListPartCode.equals(iceCream.getPartCode()) 
      || blackListPartCode.matches(iceCream.getPartCode())) { 
     matches.add(iceCream); 
     } 
    } 
    } 

    return matches; 
} 
0

場合、私== 0をし、それが一致するものを見つけ、それがアイスクリームのインデックス0の項目を削除します。リスト内の他のすべてのアイテムは、インデックスを1つずつ減らします。 この例では、最初にインデックス1を持つアイテムは、新しいインデックスが0でi == 1であるためチェックされません。

代わりに、一致するアイテムを追跡し、リストを反復した後に削除します。同じクラスのオブジェクトを比較する

0

シンプルなソリューション、次の手順を実行します

1. Open your IceCream.java file. 

2. Add following lines of code : 
     @Override 

public boolean equals(Object o) { 
    if (o == null) { 
     return false; 
    } 
    if (getClass() != o.getClass()) { 
     return false; 
    } 
    final IceCream other = (IceCream) o; 
    return this.name == other.name; 
} 

3. Now compare your IceCream class object with another IceCream class object using equals(obj) method. 

願っています!できます。 :D

0

私は最後に何が問題なのか調べます。 内部ループ内のiceCreamオブジェクトを識別する必要があります。

 for(i = 0; i<iceCreams.size(); i++) { 
     for(j = 0; j<blackListPartCodes.size(); j++) { 
      IceCream iceCream = iceCreams.get(i); 

そしてないこの方法:この方法で

for(i = 0; i<iceCreams.size(); i++) { // Object ArrayList 
     IceCream iceCream = iceCreams.get(i); 
     for(j = 0; j<blackListPartCodes.size(); j++) { 
関連する問題