• 周四. 4月 25th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

java中在for循环中remove元素时的陷阱

admin

11月 28, 2021

java.util.ConcurrentModificationException

如果删空了,会报上面这个异常

输出结果如下:
remove前集合数据:a,a,b,a,a,

remove后集合数据:a,b,a,

为什么会出现这种情况?原因是集合的大小是动态变化的,在删除第1个值为“a”的元素后,集合的大小已经发生了改变,但是i的值在删除后继续执行了加1操作,此时已经跳过了“a”元素后的元素(即原集合中第2个“a”元素)。

如下删除集合中值为“a”的元素时:

import java.util.*;
 
public class Demo {
    
    public static void main(String[] args) throws Exception {
        
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("a");
        list.add("a");
        
        System.out.print("remove前集合数据:");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+",");
        }
        
        //删除集合中值为“1”的数据
        for (int i = 0; i < list.size(); i++) {
            if ("a".equals(list.get(i))) {
                list.remove(i);
            }
        }
        
        System.out.println("");
        System.out.print("remove后集合数据:");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+",");
        }
    }
}

解决方法:

1、在执行完remove操作后,对i的值进行减1操作,即修改后的代码如下:
import java.util.*;
 
public class Demo {
    
    public static void main(String[] args) throws Exception {
        
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("a");
        list.add("a");
        
        System.out.print("remove前集合数据:");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+",");
        }
        
        //删除集合中值为“a”的数据
        for (int i = 0; i < list.size(); i++) {
            if ("a".equals(list.get(i))) {
                list.remove(i);
                i--;
            }
        }
        
        System.out.println("");
        System.out.print("remove后集合数据:");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+",");
        }
    }
}
2、用迭代器的方式删除数据
import java.util.*;
 
public class Demo {
    
    public static void main(String[] args) throws Exception {
        
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("a");
        list.add("a");
        
        System.out.print("remove前集合数据:");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+",");
        }
        
        //删除集合中值为“a”的数据
        Iterator<String> iter = list.iterator();
        while (iter.hasNext()) {
            String temp = iter.next();
            if ("a".equals(temp)) {
                iter.remove();
            }
        }
        
        System.out.println("");
        System.out.print("remove后集合数据:");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+",");
        }
    }
}

《java中在for循环中remove元素时的陷阱》有一个想法
  1. Kerassentials are natural skin care products with ingredients such as vitamins and plants that help support good health and prevent the appearance of aging skin. They’re also 100% natural and safe to use. The manufacturer states that the product has no negative side effects and is safe to take on a daily basis. Kerassentials is a convenient, easy-to-use formula. https://kerassentialsbuynow.us/

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注