如何解决有没有办法克服以下代码中的ConcurrentModificaionError或更好的实现
我想做的是创建一个在ArrayList上使用迭代器的排序函数。我想反向遍历数组,检查第i个和第(i-1)个元素中较小的一个,如果第i个元素较小,则将其移到数组的前面。
这是我的事:
import java.util.*;
// Other imports go here
// Do NOT change the class name
class Main
{
public void cq1(ArrayList<Integer> list) {
ListIterator<Integer> l1=list.listIterator();
ListIterator<Integer> l2=list.listIterator();
while(l1.hasNext()){l1.next();l2.next();}//loop to send the listIterator to the end of ArrayList
l1.previous();//make this iterator point to the second last element
while(l1.hasPrevious())
{
int a=l1.previous();
int b=l2.previous();
if (a < b)
{
list.remove(l1.previousIndex()+1);
list.add(0,a);
}
System.out.println(list);
}
}//fn
public static void main(String[] args)
{
Main obj=new Main();
int a[]={-4,16,9,1,64,25,36,4,49};
ArrayList<Integer> oe = new ArrayList();
for(int i :a)oe.add(i);
obj.cq1(oe);
}//main
}//class
问题是,在第一次迭代后出现异常:ConcurrentModificationError;弹出
我还尝试着在前进时做同样的事情,而不是在第一次迭代后弹出同样的错误。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。