[英]How to remove List iteration without ConcurrentModificationException
My allWordsList
is something like: [aaa, bbb, ccc, ddd, eee]
我的
allWordsList
类似于: [aaa, bbb, ccc, ddd, eee]
How make a copy ( tempWordsList
) of allWordsList
in 如何进行复印(
tempWordsList
的) allWordsList
在
for (String aWord : aWordsList)
without the iteration item ( ie to achieve [bbb, ccc, ddd, eee]
, then [aaa, ccc, ddd, eee]
, etc...)? 没有迭代项(即实现
[bbb, ccc, ddd, eee]
,然后[aaa, ccc, ddd, eee]
等)?
public class Anagrams {
List<String> allWordsList = new ArrayList<String>();
List<List<String>> myList = new ArrayList<List<String>>();
List<String> tempWordsList = new ArrayList<String>();
public Anagrams(String allWords) {
getWordsList(allWordsList, allWords); // getting List to copy here
getAnagramWordsList(allWordsList);
}
private void getAnagramWordsList(List<String> aWordsList) {
for (String aWord : aWordsList){
//tempWordsList.clear();
tempWordsList = aWordsList;
for (Iterator<String> iterator = tempWordsList.iterator(); iterator.hasNext();) {
String string = iterator.next();
if (string == aWord) {
// Remove the current element from the iterator and the list.
iterator.remove();
}
}
myList.add(tempWordsList);
System.out.println(aWordsList);
System.out.println(tempWordsList); //before error both lists are without first item...
}
}
}
I went through a couple of similar cases but still don't understand it well. 我经历了几个类似的案例,但仍然不太了解。
The biggest problem in your code is that tempWordsList
and aWordsList
refer to the same object. 您的代码中最大的问题是
tempWordsList
和aWordsList
引用同一对象。 Any change that you make to tempWordsList
happens to aWordsList
at the same exact time: 您对
tempWordsList
任何更改aWordsList
在同一确切时间发生在aWordsList
上:
tempWordsList = aWordsList;
Therefore, myList
will have multiple copies of the last modification of aWordList
: 因此,
myList
会有最后修改的多个副本aWordList
:
myList.add(tempWordsList);
adds the same object to myList
at each iteration of the loop. 在循环的每次迭代中将相同的对象添加到
myList
中。
In order to make aWordsList
and tempWordsList
you need to replace the assignment with a copy, like this: 为了制作
aWordsList
和tempWordsList
您需要用一个副本替换赋值,如下所示:
tempWordsList = new List<String>(aWordsList);
It should be like this :
应该是这样的:
Iterator<String> it = tempWordsList.iterator();
while(it.hasNext()){
String value = it.next();
// System.out.println("List Value:"+value);
if (value.equals(aWord)) {
it.remove();
}
}
The method that will solve your problem is the following, but it is not a good solve. 解决问题的方法如下,但这并不是一个好的解决方法。
// Solve //解决
private void getAnagramWordsList(List<String> aWordsList) {
List<Integer> toRemove = new ArrayList<Integer>();
for (String aWord : aWordsList){
//tempWordsList.clear();
tempWordsList = aWordsList;
for (int i = tempWordsList.size()-1; i > 0; i--) {
if (string.equals(tempWordsList.get(i)) {
toRemove.add(i);
}
}
for(int idx = 0; idx < toRemove.size(); idx++)
tempWordsList.remove(idx);
myList.add(tempWordsList);
System.out.println(aWordsList);
System.out.println(tempWordsList); //before error both lists are without first item...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.