Java在循环中删除list中元素的方法

先说几个通常的错误:

1、for循环遍历list

    for (int i = 0; i < list.size(); i++) {if (list.get(i).equals("del"))list.remove(i);}

2、for each循环

        for (String x : list) {if (x.equals("del"))list.remove(x);}

错误原因:

​ 没有考虑到remove方法的实现机制。

​ 在Javadoc中,明确指出list.remove()方法的实现机制是:删除索引所指向元素,并把其后的所有元素索引-1.

remove
E remove(int index)Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
Parameters:index - the index of the element to be removedReturns:the element previously at the specified positionThrows:UnsupportedOperationException - if the remove operation is not supported by this listIndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

下面的方法是正确的:

    Iterator<String> iter = list.iterator();while(iter.hasNext()){String x = iter.next();if(x.equals("del")){iter.remove();}}

可以查看Iterator的remove方法的Spec, 其中明确提到在迭代中修改list必须使用此方法。

     * Removes from the underlying collection the last element returned* by this iterator (optional operation).  This method can be called* only once per call to {@link #next}.  The behavior of an iterator* is unspecified if the underlying collection is modified while the* iteration is in progress in any way other than by calling this* method.

转载于:https://www.cnblogs.com/KarlZhang/p/8644902.html

Software Construction Series(1)相关推荐

  1. Web Client Software Factory系列(4):数据绑定和ObjectContainerDataSource控件

    概述 在Web Client Software Factory系列(3):View-Presenter模式中提到,表示器包含了响应用户事件逻辑以及一些View的状态等,在Web Client Soft ...

  2. Pandas两重山之Series(循环)

    本文属于[基础不牢,地动山摇]系列文章,介绍Pandas的Series的循环操作.再次指明两点:(1)索引和循环是进行数据增删改查及数据分析的基础,把基础掌握牢固,后面进行相关内容会事半功倍.(2)D ...

  3. Python 重写一个提取字符串中行政区划的 Series(上)

    Python 重写一个提取字符串中行政区划的 Series(上) 创作背景 思路分析 - 提取行政区划 eg1:福建省莆田市城厢区 eg2:新疆维吾尔自治区乌鲁木齐市 eg3:青海省共和县 泛化 结尾 ...

  4. Web Client Software Factory系列(3):View-Presenter模式

    概述 将一个ASP.NET站点分离为多个独立的模块,一个最大的问题就是与页面相关联的大多数业务逻辑驻留在该页面的源代码文件中,我们几乎做不到将源代码文件分为多个独立的程序集.为了真正创建独立的与站点中 ...

  5. 软件定义网络 Software Defined Network (一)概述

    软件定义网络  Software Defined Network 本文将从以下3个问题对SDN进行阐述 1.为什么要有SDN? 伴随云计算.移动互联网和物联网的蓬勃兴起,应用与业务日益多元,而且快速且 ...

  6. Pandas两重山之Series(索引)

    Pandas概述 本文属于[基础不牢,地动山摇]系列文章,介绍Pandas的Series的概念及索引方式. 每个Pythoner都听说过Pandas,Pandas库的建立理念是:快速.强大.灵活.易用 ...

  7. Taylor Series(泰勒级数)

    文章目录 引子 Why Taylor Series 函数$cos(x)$的近似 二阶近似 二阶近似$\cos(x)$总结 增加阶数近似 继续延伸 泰勒多项式 对任意函数近似 泰勒级数的几何意义 其他级 ...

  8. Pandas-数据结构-Series(一):创建Series【①由字典创建;②由一维数组创建;③由标量创建】【Series的属性:index、values、name】

    Pandas中一共有三种数据结构,分别为:Series.DataFrame和MultiIndex(老版本中叫Panel ).其中: Series是一维数据结构: DataFrame是二维的表格型数据结 ...

  9. Pandas-数据结构-Series(二):Series的索引【下标索引、标签索引、切片索引、布尔型索引】

    一.下标索引 位置下标,类似序列 位置下标从0开始 输出结果为numpy.float格式, 可以通过float()函数转换为python float格式 numpy.float与float占用字节不同 ...

最新文章

  1. 7 款可替代 top 命令的工具!(二)
  2. 我的世界java1.15.2光影_2.世界摄影简史
  3. 论流量平台(交易内容)生死劫——币看流量生意正在进入正循环
  4. php : RBAC 基于角色的用户权限控制-表参考
  5. python类中的函数_python类中的函数问题
  6. 父亲节html代码,jquery怎么获取父亲节点?
  7. Boost:演示如何计算加权统计信息
  8. ASP.NET 网页之间传递值
  9. python的cmd下小白开发应用教程
  10. HTTP Session 的工作原理以及几个思维扩展
  11. hive内部表与外部表入门
  12. python——item()返回可遍历的(键,值)元组数据
  13. 2007年春节,祝朋友们:身体健康,万事如意! 度过一个愉快春节!
  14. win7共享文件夹设置无密码
  15. 构建Spring Cloud项目找不到Application类
  16. snort:Packet结构体详解(留坑)
  17. [机器学习必知必会]泛化误差率的偏差-方差分解
  18. win7网上邻居_win7系统网上邻居在哪
  19. Mysql B+树索引的使用
  20. AUTOSAR Diagnostic

热门文章

  1. PHP制作钱包银行金额数字展示功能实例
  2. Java笔记-spring-rabbitmq中queue设置argument(添加x-message-ttl)
  3. 前端笔记-echarts加载bmap地图
  4. C++设计模式-命令模式
  5. js符号输入不可用_JS 控制非法字符的输入代码
  6. bi power 两个日期挑较早的日期_功率 BI 中的时间智能:利用时间
  7. matlab7.0编辑运行,手把手解答win10系统运行matlab7.0时提示Runtime error的操作方案
  8. jenkins -ant-svn 自动化持续部署——web应用war包 ant build.xml配置
  9. 完全背包——方案个数 UVA11137 Ingenuous Cubrency
  10. 计组之存储系统:8、Cache写策略(全写法、写回法、写分配法、非写分配法、多级Cache)