以下のプログラムからConcurrentModificationException
を解決する方法を教えてください。 最初の要素が"Znk"
のリストが必要です。ソートされたリストはそれに続いてリストされます。同時変更例外
同じ繰り返しで追加したり削除したりしているので、私は理解しています。しかし、どのようにしてこれを解決し、望ましい出力を得るか。
public class ListSwapIndex {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> swapIndex = new ArrayList<String>();
ArrayList<String> swapIndextemp = new ArrayList<String>();
swapIndex.add("Ank");
swapIndex.add("Znk");
swapIndex.add("Bnk");
swapIndex.add("Dnk");
swapIndex.add("Enk");
swapIndex.add("Lnk");
for (String string : swapIndex) {
if(string.equals("Znk")){
swapIndextemp.add(string);
swapIndex.remove(string);
}
}
swapIndextemp.addAll(swapIndex);
System.out.println(swapIndextemp);
}
}
いいえ、配列を同時に変更しているからです。代わりにベクトルまたは同期arraylistを使用してください。 – Ioan
参照[this](http://stackoverflow.com/questions/18448671/how-to-avoid-concurrentmodificationexception-while-removing-elements-from-arr)重複可能性「拡張forループでアーカイブできません」 – malatesh