私は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
にあるフィールドがまだ存在:この経由 。
'しかし、あなたが何を意味するcorrectly'動作しませんか? –
私は推測させてください: 'ConcurrentModificationException'。 SOとウェブ全体で簡単に検索できます。また、一般的に、 'remove(Object)'を使うと、リストを反復処理する必要があるので、大したことはありません。あるいは、正しいものを削除していない場合は、 'equals'実装を示すことが有用かもしれません。 –
私は質問を編集しました。 –