List遍历时删除元素中存在的问题

下面列举常见的三中遍历list是删除元素的方法。

​
public class ListTest5 {

	public static void main(String[] args) {
		
		ArrayList<String> list = new ArrayList<>();
		list.add("java");
		list.add("c#");
		list.add("c++");
		list.add("c++");
		list.add("php");
		
//		System.out.println(remove1(list));
//		System.out.println(remove2(list));
		System.out.println(remove3(list));
		
	}
	
	public static List remove1(List list){
		//for循环遍历删除,for循环删除元素后,后面的元素会前移补齐,导致没有被遍历
		for(int i = 0; i < list.size(); i++){
			String s = (String) list.get(i);
			if(s.equals("c++")){
				list.remove(i);
			}
		}
		return list;
	}
	
	public static List remove2(ArrayList<String> list){
		//foreach循环遍历删除,会报错,因为内部迭代时不允许要求容器结构不发生变化
		for(String s : list){
			if(s.equals("c++")){
				list.remove(s);
			}
		}
		return list;
	}
	
	public static List remove3(ArrayList<String> list){
		Iterator<String> iterator = list.iterator();
		while(iterator.hasNext()){
			if(iterator.next().equals("c++")){
				iterator.remove();
			}
		}
		return list;
	}
}

​

1、remove1方法

remove1方法只能删除第一个“c++”,删除元素时涉及到数组元素的移动,所以在删除后,将后一个元素移动(也就是第二个字符串 "c++")至当前位置,导致下一次循环遍历时后一个字符串没有被遍历到,所以无法删除。

ArrayList源码中remove方法如下:

    public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }

    /*
     * Private remove method that skips bounds checking and does not
     * return the value removed.
     */
    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; // clear to let GC do its work
    }

2、remove2方法

remove2方法会报错如下:

因为迭代器内部会维护一些索引位置数据,要求在迭代过程中容器不能发生结构性变化。

 

参考资料

https://mp.weixin.qq.com/s?__biz=MzI3ODc3NzQ4NQ==&mid=2247483951&idx=1&sn=9e5641c8a6524c13acf367a8dd1c0b98&chksm=eb5099cddc2710dbfed8e2d8a5aeaf3c20c53fe2256b2e5b7fa23649922c0e2a8f500abafc5b&scene=21#wechat_redirect

猜你喜欢

转载自blog.csdn.net/zh137289/article/details/88193800