Java中List.remove()方法的bug

简介: 一、在Java中List.remove方法有个bug 1.看第一个针对Object的boolean remove(Object var1);看一下API接口,在看一下实现类 实现类:/** ...

一、在Java中List.remove方法有个bug
1.看第一个针对Object的

boolean remove(Object var1);

看一下API接口,在看一下实现类
这里写图片描述
实现类:

/**
     * {@inheritDoc}
     *
     * <p>This implementation iterates over the collection looking for the
     * specified element.  If it finds the element, it removes the element
     * from the collection using the iterator's remove method.
     *
     * <p>Note that this implementation throws an
     * <tt>UnsupportedOperationException</tt> if the iterator returned by this
     * collection's iterator method does not implement the <tt>remove</tt>
     * method and this collection contains the specified object.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws ClassCastException            {@inheritDoc}
     * @throws NullPointerException          {@inheritDoc}
     */
    public boolean remove(Object o) {
        Iterator<E> it = iterator();
        if (o==null) {
            while (it.hasNext()) {
                if (it.next()==null) {
                    it.remove();
                    return true;
                }
            }
        } else {
            while (it.hasNext()) {
                if (o.equals(it.next())) {
                    it.remove();
                    return true;
                }
            }
        }
        return false;
    }

2.看第二个针对索引

E remove(int var1);

这里写图片描述
看一下实现类

/**
     * {@inheritDoc}
     *
     * <p>This implementation always throws an
     * {@code UnsupportedOperationException}.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws IndexOutOfBoundsException     {@inheritDoc}
     */
    public E remove(int index) {
        throw new UnsupportedOperationException();
    }

二、在实际工作中可能会出现的bug
看一下代码

package net.println.kotlin.chapter4;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:wangdong
 * @description:看一下list的bug
 */
public class Bug {
    public static void main(String[] args) {
        List<Integer> integerList = new ArrayList<>();
        integerList.add(23);
        integerList.add(233);
        integerList.add(243);
        integerList.add(235);
        integerList.add(5);
        integerList.add(50);
        integerList.add(500);

        System.out.println(integerList);
        //这里会出现一个bug,当list中存在与索引相同的元素的时候,使用remove,就不知道会删除掉那个了
        integerList.remove(1);
        //这个5的索引是4,这样写可能就删除了索引为5的50,也可能是删除了5的元素
        integerList.remove(5);
        System.out.println(integerList);

    }
}
目录
相关文章
|
4天前
|
分布式计算 DataWorks Java
DataWorks操作报错合集之在使用MaxCompute的Java SDK创建函数时,出现找不到文件资源的情况,是BUG吗
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
18 0
|
13天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
47 3
|
4天前
|
Java
判断不为空和不为空串的方法java
判断不为空和不为空串的方法java
|
4天前
|
Java API
【亮剑】Java的List,如何删除重复的元素,教你三个方法搞定!
【4月更文挑战第30天】本文介绍了三种Java中删除List重复元素的方法:1) 使用HashSet,借助其不允许重复值的特性;2) 利用Java 8 Stream API的distinct()方法;3) 对自定义对象重写equals()和hashCode()。每种方法都附带了代码示例,帮助理解和应用。
|
5天前
|
IDE Java 开发工具
基于Java程序设计的实验教学方法优化与实践
基于Java程序设计的实验教学方法优化与实践
15 1
|
7天前
|
存储 Java 索引
【JAVA】HashMap的put()方法执行流程
【JAVA】HashMap的put()方法执行流程
|
7天前
|
存储 算法 Java
【JAVA】Java 中 Set集合常用方法
【JAVA】Java 中 Set集合常用方法
|
10天前
|
Java
Java 与垃圾回收有关的方法
Java 与垃圾回收有关的方法
|
10天前
|
Java
Java基础&方法
Java基础&方法
|
10天前
|
Java 编译器
Java 方法
4月更文挑战第19天