删除ArrayList的奇数项或偶数项
List<String> list = new ArrayList<String>();
list.add("zhangcheng");
list.add("zhangchong");
list.add("gaofucheng");
list.add("haha");
马戏团公约Iterator<String> it = list.iterator();
int b = list.size();
for(int i = 0;i<b;i++){
<();
if((i&1)!=1){
/
/删除奇数项
宫2主题曲
}
}
注意这⾥⽤的是it.remove().
如果⽤的ve()⽅法会改变ArrayList中的
/**
* The number of times this list has been <i>structurally modified</i>.
* Structural modifications are those that change the size of the
* list, or otherwise perturb it in such a fashion that iterations in
* progress may yield incorrect results.
*
* <p>This field is used by the iterator and list iterator implementation
* returned by the {@code iterator} and {@code listIterator} methods.
* If the value of this field changes unexpectedly, the iterator (or list
* iterator) will throw a {@code ConcurrentModificationException} in
* response to the {@code next}, {@code remove}, {@code previous},
* {@code set} or {@code add} operations.  This provides
* <i>fail-fast</i> behavior, rather than non-deterministic behavior in
* the face of concurrent modification during iteration.
*
* <p><b>Use of this field by subclasses is optional.</b> If a subclass
* wishes to provide fail-fast iterators (and list iterators), then it
* merely has to increment this field in its {@code add(int, E)} and
克拉恋人歌曲
* {@code remove(int)} methods (and any other methods that it overrides
* that result in structural modifications to the list).  A single call to
* {@code add(int, E)} or {@code remove(int)} must add no more than
* one to this field, or the iterators (and list iterators) will throw
* bogus {@code ConcurrentModificationExceptions}.  If an implementation
* does not wish to provide fail-fast iterators, this field may be
* ignored.
*/
protected transient int modCount = 0;
modCount属性的值,⽽在
public E remove(int index) {
rangeCheck(index);
checkForComodification();
黄宏案E result = ve(parentOffset + index);
this.size--;
return result;
是谁在等天明是谁在数星星什么歌
}
⽅法中的checkForComodification();
private void checkForComodification() {
if (dCount != dCount)
我爱台妹throw new ConcurrentModificationException();
}
会对该属性做判断,于是抛出了ConcurrentModificationException的异常