正确在遍历中删除List元素
最近在写代码的时候遇到了遍历时删除List元素的问题,在此写一篇博客记录一下。
一般而言,遍历List元素有以下三种方式:
- 使用普通for循环遍历
- 使用增强型for循环遍历
- 使用iterator遍历
使用普通for循环遍历
- public class Main {
- public static void main(String[] args) throws Exception {
- List<Integer> list = new ArrayList<>();
- for (int i = 0; i < 5; i++)
- list.add(i);
- // list {0, 1, 2, 3, 4}
- for (int i = 0; i < list.size(); i++) {
- // index and number
- System.out.print(i + " " + list.get(i));
- if (list.get(i) % 2 == 0) {
- list.remove(list.get(i));
- System.out.print(" delete");
- i--; // 索引改变!
- }
- System.out.println();
- }
- }
- }
结果如下:
使用增强型for循环遍历
- public class Main {
- public static void main(String[] args) throws Exception {
- List<Integer> list = new ArrayList<>();
- for (int i = 0; i < 5; i++)
- list.add(i);
- // list {0, 1, 2, 3, 4}
- for (Integer num : list) {
- // index and number
- System.out.print(num);
- if (num % 2 == 0) {
- list.remove(num);
- System.out.print(" delete");
- }
- System.out.println();
- }
- }
- }
结果如下:
使用iterator遍历
- public class Main {
- public static void main(String[] args) throws Exception {
- List<Integer> list = new ArrayList<>();
- for (int i = 0; i < 5; i++)
- list.add(i);
- // list {0, 1, 2, 3, 4}
- Iterator<Integer> it = list.iterator();
- while (it.hasNext()) {
- // index and number
- int num = it.next();
- System.out.print(num);
- if (num % 2 == 0) {
- it.remove();
- System.out.print(" delete");
- }
- System.out.println();
- }
- }
- }
结果如下:
使用普通for循环遍历
- public class Main {
- public static void main(String[] args) throws Exception {
- List<Integer> list = new CopyOnWriteArrayList<>();
- for (int i = 0; i < 5; i++)
- list.add(i);
- // list {0, 1, 2, 3, 4}
- for (int i = 0; i < list.size(); i++) {
- // index and number
- System.out.print(i + " " + list.get(i));
- if (list.get(i) % 2 == 0) {
- list.remove(list.get(i));
- System.out.print(" delete");
- i--; // 索引改变!
- }
- System.out.println();
- }
- }
- }
结果如下:
使用增强型for循环遍历
- public class Main {
- public static void main(String[] args) throws Exception {
- List<Integer> list = new CopyOnWriteArrayList<>();
- for (int i = 0; i < 5; i++)
- list.add(i);
- // list {0, 1, 2, 3, 4}
- for (Integer num : list) {
- // index and number
- System.out.print(num);
- if (num % 2 == 0) {
- list.remove(num);
- System.out.print(" delete");
- }
- System.out.println();
- }
- }
- }
结果如下:
使用iterator遍历
- public class Main {
- public static void main(String[] args) throws Exception {
- List<Integer> list = new CopyOnWriteArrayList<>();
- for (int i = 0; i < 5; i++)
- list.add(i);
- // list {0, 1, 2, 3, 4}
- Iterator<Integer> it = list.iterator();
- while (it.hasNext()) {
- // index and number
- int num = it.next();
- System.out.print(num);
- if (num % 2 == 0) {
- it.remove();
- System.out.print(" delete");
- }
- System.out.println();
- }
- }
- }
结果如下:
正确在遍历中删除List元素的更多相关文章
- List在遍历中删除t元素
法一:使用普通for循环遍历 注意: 1.从头开始循环,每次删除后 i 减一. 2.从尾开始循环. public class Main { public static voi ...
- java 在循环中删除数组元素
在写代码中经常会遇到需要在数组循环中删除数组元素的情况,但删除会导致数组长度变化. package com.fortunedr.thirdReport; import java.util.ArrayL ...
- ES6数组中删除指定元素
知识点: ES6从数组中删除指定元素 findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引.否则返回-1. arr.splice(arr.findIndex(item => ...
- C#实现在foreach遍历中删除集合中的元素(方法总结)
目录 方法一:采用for循环,并且从尾到头遍历 方法二:使用递归 方法三:通过泛型类实现IEnumerator 在foreach中删除元素时,每一次删除都会导致集合的大小和元素索引值发生变化,从而导致 ...
- 【坑】Java中遍历递归删除List元素
运行环境 idea 2017.1.1 需求背景 需要做一个后台,可以编辑资源列表用于权限管理 资源列表中可以有父子关系,假设根节点为0,以下以(父节点id,子节点id)表示 当编辑某个资源时,需要带出 ...
- Java集合类ArrayList循环中删除特定元素
在项目开发中,我们可能往往需要动态的删除ArrayList中的一些元素. 一种错误的方式: <pre name="code" class="java"&g ...
- 如何python循环中删除字典元素
//下面这行就是在循环中遍历删除字典元素的方法! for i in list(dictheme2.keys()): if dictheme2[i]<self.countFortheme: dic ...
- LeetCode 82,考察你的基本功,在有序链表中删除重复元素II
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第51篇文章,我们来看LeetCode第82题,删除有序链表中的重复元素II(Remove Duplicates ...
- python中删除某个元素的3种方法
python中关于删除list中的某个元素,一般有三种方法:remove.pop.del 1.remove: 删除单个元素,删除首个符合条件的元素,按值删除 举例说明: >>> st ...
随机推荐
- python爬虫requests json与字典对象互相转换
import requests import json ''' json.loads(json_str) json字符串转换成字典 json.dumps(dict) 字典转换成json字符串 ''' ...
- JavaScript作用域那些事
作用域 (1).作用域也叫执行环境(execution context)是JavaScript中一个重要的概念.执行环境定义了变量或函数有权访问的其他数据,决定了它们各自的行为.在JavaScript ...
- MySQL/上
MySQL操作/上 一.视图 视图表是一个虚拟表(非真实存在),其本质是[根据sql语句获取动态的数据集,并为其命名],用户使用表只需使用(名称)即可获取结果集,并可以将其当做表来使用. 1.创建视图 ...
- MySQL基础操/下
MySQL基础操 一.自增补充 desc (表名)t1: 查看表格信息内容 表的信息 show create table t1(表名):也是查看信息,还不多是横向查看 show create tabl ...
- Java高级篇(三)——JDBC数据库编程
JDBC是连接数据库和Java程序的桥梁,通过JDBC API可以方便地实现对各种主流数据库的操作.本篇将介绍一下如何使用JDBC操作数据库(以MySQL为例). 一.JDBC JDBC制定了统一访问 ...
- Treap讲解
Treap讲解 上一篇blog提出了Treap这个算法,在这里我就要详细讲解. 首先,我们可以从字面上理解这个算法,Treap这个单词是由Tree和Heap两个单词构成的,所以它的性质就很好理解了,明 ...
- p2p项目总结
1.关于ajax请求所要注意的地方:$.psot(url,json,callback,type) (1)url路径问题,在html中写绝对路径不能用EL表达式,EL表达式只能在jsp中使用 (2)js ...
- [C#]设计模式-简单工厂-创建型模式
在设计模式当中有三大工厂,分别是 简单工厂.抽象工厂.工厂方法 这三种创建实例的设计模式,这里先从简单工厂将其,从名字就可以看出这是这三种工厂模式当中最为简单的一种实现. 简单工厂一般由以下几个对象组 ...
- [LeetCode] Max Area of Island 岛的最大面积
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- ActiveMQ(七)_伪集群和主从高可用使用(转)
本文转自: https://www.cnblogs.com/gossip/p/5977489.html 一.本文目的 介绍如何在同一台虚拟机上搭建高可用的Activemq服务,集群数量 ...