Java子列表ConcurrentModificationException();
我已经定义了ArrayList:Java子列表ConcurrentModificationException();,java,android,arraylist,sublist,Java,Android,Arraylist,Sublist,我已经定义了ArrayList: ArrayList<String> numbers = new ArrayList<String>(); 在我再次尝试使用子列表之前,它可以正常工作: numbersh.remove(0); 我收到/来自AbstractList.class/: throw new ConcurrentModificationException(); 为了更好地理解,请想象以下数组列表/numbers/: [1,2,3,4,5,6,7,8]/个数/
ArrayList<String> numbers = new ArrayList<String>();
在我再次尝试使用子列表之前,它可以正常工作:
numbersh.remove(0);
我收到/来自AbstractList.class/:
throw new ConcurrentModificationException();
为了更好地理解,请想象以下数组列表/numbers/:
[1,2,3,4,5,6,7,8]/个数/
及其
List numbersh=numbersh.子列表(3,5)代码>由[4,5,6]组成
我现在做的是number.remove(6)代码>超出子列表范围,应导致
[1,2,3,4,5,6,8]/个数/
然后我尝试numbersh.remove(0)
,这将导致:
[1,2,3,5,6,8]/个数/
我有点迷路了。有什么想法吗?您忘记了subList()不会返回新列表,而只是原始列表的一部分
public List<E> subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, 0, fromIndex, toIndex);
}
SubList(AbstractList<E> parent,
int offset, int fromIndex, int toIndex) {
this.parent = parent;
this.parentOffset = fromIndex;
this.offset = offset + fromIndex;
this.size = toIndex - fromIndex;
this.modCount = ArrayList.this.modCount;
}
公共列表子列表(int-fromIndex,int-toIndex){
子列表范围检查(从索引到索引、大小);
返回新的子列表(this,0,fromIndex,toIndex);
}
子列表(抽象列表父级,
整数偏移量,整数从索引,整数到索引){
this.parent=parent;
this.parentOffset=fromIndex;
this.offset=offset+fromIndex;
this.size=toIndex-fromIndex;
this.modCount=ArrayList.this.modCount;
}
如果您需要单独修改和访问列表和子列表,则需要创建原始列表的深度副本。请发布实际代码。人类的语言太模棱两可了。@PhilippSander-错了。查看ArrayList.subList()的代码,您将看到返回的子列表不是副本。”如果以任何方式(而不是通过返回的列表)修改支持列表,则此方法返回的列表的语义将变得未定义。(结构修改是指那些改变列表大小的修改,或者以某种方式干扰列表,使得正在进行的迭代可能产生不正确的结果。)“直接从JavaDoc开始。看起来你正在做JavaDoc告诉你不要做的事情。你到底希望在这里发生什么?
throw new ConcurrentModificationException();
public List<E> subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, 0, fromIndex, toIndex);
}
SubList(AbstractList<E> parent,
int offset, int fromIndex, int toIndex) {
this.parent = parent;
this.parentOffset = fromIndex;
this.offset = offset + fromIndex;
this.size = toIndex - fromIndex;
this.modCount = ArrayList.this.modCount;
}