您的位置:首页 > 编程语言 > Java开发

List集合删除元素,该怎么删除?

2016-07-04 22:29 357 查看
package com.zz.web.controller;

import java.util.ArrayList;
import java.util.Iterator;

public class TestDelList {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> lists = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("b");
list.add("b");
list.add("e");
list.add("b");
list.add("d");
lists.addAll(list);
//此处先使用list的remove()方法
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals("b")) {
list.remove(i);
}
System.out.println(list.toString() + list.size());
}
//下面使用Iterator的remove()方法是跟上面做一个对比
Iterator<String> it = lists.iterator();
while (it.hasNext()) {
String string = (String) it.next();
if (string.equals("b")) {
it.remove();
}
System.out.println("w" +lists.toString()+ lists.size());
}
}
}


运行后的结果是:
[a, b, c, b, b, e, b, d]8
[a, c, b, b, e, b, d]7
[a, c, b, e, b, d]6
[a, c, b, e, b, d]6
[a, c, b, e, d]5
w[a, b, c, b, b, e, b, d]8
w[a, c, b, b, e, b, d]7
w[a, c, b, b, e, b, d]7
w[a, c, b, e, b, d]6
w[a, c, e, b, d]5
w[a, c, e, b, d]5
w[a, c, e, d]4
w[a, c, e, d]4


大家发现没有,如果使用list的remove()方法,想要把list集合里的b元素删除,会导致上述的情况发生(该情况是需要删除的元素都是相邻的元素,用代码解释就是list.get(i).equals(list.get(i+1))==true, 这样在移除b(i=3时)元素的时候该节点都被删除了,后面的元素就会往前移动一位,当循环下一个i=2的时候list.get(4)的值不是b元素了而是e元素);而使用迭代器Iteratoer去遍历该list集合用迭代器的remove方法就不会导致该问题出现

为什么用list的remove()就不可以呢?我找了源码才发现它实际上调用的是另一个方法:

private void fastRemove(int index)
{
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index, numMoved);
elementData[--size] = null;
}


看出来什么了吗?有一个数组复制的方法:

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

src:源数组;

srcPos:源数组要复制的起始位置;

dest:目的数组;

destPos:目的数组放置的起始位置;

length:复制的长度。

注意:src and dest都必须是同类型或者可以进行转换类型的数组。

由此看出为什么会前移一位吗?

(其实最主要的原因不在这里,最主要的原因是其中有一个计数器modCount还有期望被修改次数expectedModCount),当使用list.remove()的时候,该modCount会加1而expectedModCount是0,Iterator.remove()却不会,是因为在Iterator的remove方法中它将modCount=expectedModCount)具体见下面的文章,我就不一一解释了。仔细看完保证你有收获!

更多详情请参照我参考的关于list和Iterator的remove方法区别文章Blog地址 ↓↓↓↓↓

http://www.cnblogs.com/dolphin0520/p/3933551.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息