Java使用list集合remove需要注意的事项

在实际开发中有时候会碰到这样的场景,需要将一个list集合中的某些特定的元素给删除掉,这个时候用可以用List提供的remove方法来实现需求。

List中的remove方法传入的参数可以是集合的下标,也可以是集合中一个元素,也可以是一个集合

错误使用示例一

这里我使用的是增强for循环,会发现直接报错。

List集合的一个特点是它其中的元素时有序的,也就是说元素的下标是根据插入的顺序来的,在删除头部或者中间的一个元素后,后面的元素下标会往前移动。循环的时候就会出现问题。

@Data
@AllArgsConstructor
public class Person {
    private String id;
    private String name;

    public static void main(String[] args) {
        List<Person> lists = new ArrayList<>();
        lists.add(new Person("1", "张三"));
        lists.add(new Person("2", "王五"));
        lists.add(new Person("3", "李六"));
        lists.add(new Person("4", "哈哈"));

        for (Person person4 : lists) {
            if (person4.getId().equals("2")) {
                lists.remove(person4);
            }
        }
    }
}

解决方案一:不要用for-each遍历,换成迭代器遍历,并且不要用list.remove()方法移除对象,用迭代器的方法iterator.remove()移除对象。

@Data
@AllArgsConstructor
public class Person {
    private String id;
    private String name;

    public static void main(String[] args) {
        List<Person> lists = new ArrayList<>();
        lists.add(new Person("1", "张三"));
        lists.add(new Person("2", "王五"));
        lists.add(new Person("3", "李六"));
        lists.add(new Person("4", "哈哈"));

        Iterator<Person> iterator = lists.iterator();
        while (iterator.hasNext()){
            Person next = iterator.next();
            if(next.getId().equals("2")){
                iterator.remove();
            }
        }

        lists.forEach(item-> System.out.println(item));
    }
}

解决方案二:在循环中(比如for循环)使用remove方法时,注意要从List集合的最后一个元素开始遍历。

@Data
@AllArgsConstructor
public class Person {
    private String id;
    private String name;

    public static void main(String[] args) {
        List<Person> lists = new ArrayList<>();
        lists.add(new Person("1", "张三"));
        lists.add(new Person("2", "王五"));
        lists.add(new Person("3", "李六"));
        lists.add(new Person("4", "哈哈"));

        for (int i = lists.size() - 1; i >= 0; i--) {
            if (lists.get(i).getId().equals("2")) {
                lists.remove(i);
            }
        }

        lists.forEach(item-> System.out.println(item));
    }
}

错误使用示例二

这个示例当中没有使用增强for,而是普通的循环,没有报错,但是结果不是我们想要的结果。

public static void main(String[] args) {
    List<String> strList = new ArrayList<>();
    strList.add("a");
    strList.add("b");
    strList.add("c");
    strList.add("d");
    strList.add("e");
    strList.add("f");
    for (int i = 0; i < strList.size(); i++) {
        if (i % 2 == 0) {
            strList.remove(i);
        }
    }
    for (String str : strList) {
        System.out.print(str + " ");
    }
}

这个示例是我要把集合坐标为2的元素给删除掉,按正常来说输出b d f 才是我们想要的结果,显然结果是不对的。

分析结果原因

a b c d e f
0 1 2 3 4 5 删除0坐标的a,然后后面元素向前移动

b c d e f
0 1 2 3 4   当i=1的时候不删除,这时候i为2,删除d

b c e f
0 1 2 3	    删除完d之后,后面向前移动,当i为3时候不用删除,循环结束。

注意:假如使用增强for就会报上面案例当中的错误。

for (String a : strList) {
    if (a.equals("a")) {
        strList.remove(a);
    }
}

解决方案:倒着循环list即可。

public static void main(String[] args) {
    List<String> strList = new ArrayList<>();
    strList.add("a");
    strList.add("b");
    strList.add("c");
    strList.add("d");
    strList.add("e");
    strList.add("f");
    for (int i = strList.size() - 1; i >= 0; i--) {
        if (i % 2 == 0) {
            strList.remove(i);
        }
    }
    for (String str : strList) {
        System.out.print(str + " ");
    }
}
  • 9
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怪 咖@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值