System.out.println(list);
输出结果:[1, 2, 4]
4、foreach遍历List删除元素–错误
!!!
for(Integer i:list){
if(i==3) list.remove(i);
}
System.out.println(list);
抛出异常:java.util.ConcurrentModificationException
foreachList.iterator()
源码着手分析,跟踪iterator()
方法,该方法返回了 Itr 迭代器对象。
public Iterator iterator() {
return new Itr();
}
Itr 类定义如下:
private class Itr implements Iterator {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings(“unchecked”)
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
通过代码我们发现 Itr 是 ArrayList 中定义的一个私有内部类,在 next、remove方法中都会调用checkForComodification 方法,该方法的 作用是判断 modCount != expectedModCount
是否相等,如果不相等则抛出ConcurrentModificationException
异常。
每次正常执行 remove 方法后,都会对执行expectedModCount = modCount
赋值,保证两个值相等,那么问题基本上已经清晰了,在 foreach 循环中执行 list.remove(item);
,对 list 对象的 modCount 值进行了修改,而 list 对象的迭代器的 expectedModCount 值未进行修改,因此抛出了ConcurrentModificationException
异常。
5、迭代删除List元素–正确
!
java中所有的集合对象类型都实现了Iterator接口,遍历时都可以进行迭代:
Iterator it=list.iterator();
while(it.hasNext()){
if(it.next()==3){
it.remove();
}
}
System.out.println(list);
输出结果:[1, 2, 4]
Iterator.remove()
方法会在删除当前迭代对象的同时,会保留原来元素的索引。所以用迭代删除元素是最保险的方法,建议大家使用List过程
中需要删除元素时,使用这种方式。学习资料:Java进阶视频资源
6、迭代遍历,用list.remove(i)方法删除元素–错误
!!!
Iterator it=list.iterator();
while(it.hasNext()){
Integer value=it.next();
if(value==3){
list.remove(value);
}
}
System.out.println(list);
抛出异常:java.util.ConcurrentModificationException
,原理同上述方法4.
7、List删除元素时,注意Integer类型和int类型的区别.
上述Integer的list,直接删除元素2,代码如下:
list.remove(2);
System.out.println(list);
输出结果:[1, 2, 3, 4]
可以看出,List删除元素时传入数字时,默认按索引删除。如果需要删除Integer对象,调用remove(object)
方法,需要传入Integer类型,代码如下:
list.remove(new Integer(2));
总结
谈到面试,其实说白了就是刷题刷题刷题,天天作死的刷。。。。。
为了准备这个“金三银四”的春招,狂刷一个月的题,狂补超多的漏洞知识,像这次美团面试问的算法、数据库、Redis、设计模式等这些题目都是我刷到过的
并且我也将自己刷的题全部整理成了PDF或者Word文档(含详细答案解析)
66个Java面试知识点
架构专题(MySQL,Java,Redis,线程,并发,设计模式,Nginx,Linux,框架,微服务等)+大厂面试题详解(百度,阿里,腾讯,华为,迅雷,网易,中兴,北京中软等)
算法刷题(PDF)
细答案解析)**
[外链图片转存中…(img-2jZShJtd-1719282068126)]
66个Java面试知识点
架构专题(MySQL,Java,Redis,线程,并发,设计模式,Nginx,Linux,框架,微服务等)+大厂面试题详解(百度,阿里,腾讯,华为,迅雷,网易,中兴,北京中软等)
[外链图片转存中…(img-3D8V9p4o-1719282068127)]
算法刷题(PDF)
[外链图片转存中…(img-zcTjlQog-1719282068127)]