Java中List.remove()方法的bug
一、在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);
}
}

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
【TensorFlow开发者峰会】重磅发布TensorFlow.js,完全在浏览器运行机器学习
Jeff Dean主旨演讲:用超强大的计算力,替代ML 北京时间3月31日举行的2018 TensorFlow 开发者峰会上,Google Brain负责人、谷歌高级研究员Jeff Dean、TensorFlow 总监 Rajat Monga等人进行了Keynote演讲。 Jeff Dean也是目前最受欢迎的深度学习框架TensorFlow的缔造者之一,他曾经就TensorFlow的高级使用,如何用TensorFlow实现大规模机器学习等问题做过多次演讲。今年初,Jeff Dean执笔撰写谷歌大脑成绩单,介绍了TensorFlow取得的一系列重要更新。 图:TensorFlow 用户的分布地图 TensorFlow 是 GitHub 上的第一个机器学习平台,也是 GitHub 上的五大软件库之一,被许多公司和组织所使用,包括 GitHub
-
下一篇
逸鹏说道:读王阳明、曾国藩有所感
推荐大家听听,相信大家会有部分感同身受的,这一年来经历了很多事情,很多东西也看透了,很多东西也看淡了。 想想曾几何时的挑灯夜战,曾几何时的抱病修书,星星点点,林林总总,以前我曾觉得这些华而不实的东西真心没必要,大白话一句的为什么要绕来绕去,直来直去多好。思极慎思的慢慢发现,其实我错了,大错特错,那些所谓的牛掰又能怎样,技术再牛有些事情也不能改变,技术再转有些人情还是要处,退一万步讲:“读书可以使人明事理,可以让人在这闹市中有一份静的心”,你是否多年未读完一本非技术方面的书了?你是否现在连读一篇文章的耐心都没了呢?其实这就是佛家里面的“着相”了,其实没有那么多条条框框,万事凭心即可。读完王阳明才发现原来这个就是致良知。。。 那何为技术?古时候实用主义的学说都是以“为生活便利”为前提的。现代技术一次次升级,小到量子夸克,夸克下不是没有了,而是科学家发现总这么追究下去其实也没有啥意思,于是又回到了生活。一个人的精力是有限的,就算正当青年精力无穷也是有力竭之时。如旧力已发而新力未生的时候何尝不是最危险的时候?小说里面高手就喜欢找这样的空隙来借力打力。 我理解的技术是:“于生活有利,于百姓...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- Red5直播服务器,属于Java语言的直播服务器
- SpringBoot2全家桶,快速入门学习开发网站教程
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS8编译安装MySQL8.0.19
- MySQL数据库在高并发下的优化方案
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池