正确在遍历中删除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 ...
随机推荐
- Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允许有内容。
Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber ...
- 分布式服务框架HSF
最近在读阿里巴巴中台战略思想与架构这本书,so和大家分享一些我get到的东东. HSF是阿里巴巴内部的分布式服务框架,这个大家都很熟悉了,先上一张HSF的工作原理图: 这个图说明了HSF框架中每个组件 ...
- Linux命令(持续更新中)
命令名 用法 安装上传下载 yum install lrzsz rz上传文件,sz下载文件 压缩 解压文件 tar -zxvf 文件名 压缩文件 tar -zcvf 文件名 删除非空目录: rm ...
- svn介绍和安装
什么是SVN呢,作用是什么: SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS/CVS,它采取了分支管理系统,它的设计目标就是取代CVS.SVN就是用于多个人共同开 ...
- Spark:将RDD[List[String,List[Person]]]中的List[Person]通过spark api保存为hdfs文件时一直出现not serializable task,没办法找到"spark自定义Kryo序列化输入输出API"
声明:本文转自<在Spark中自定义Kryo序列化输入输出API> 在Spark中内置支持两种系列化格式:(1).Java serialization:(2).Kryo seriali ...
- Struts(二十六):文件上传
表单的准备 想要使用html表单上传一个或多个文件 1.须把html表单的enctype属性设置为multipart/form-data 2.须把html表单的method属性设置为post 3.须添 ...
- POJ-1287 Networking---裸的不能再裸的MST
题目链接: https://vjudge.net/problem/POJ-1287 题目大意: 模板 #include<iostream> #include<cstdio> # ...
- Tensorflow计算模型 —— 计算图
转载自:http://blog.csdn.net/john_xyz/article/details/69053626 Tensorflow是一个通过计算图的形式来表述计算的编程系统,计算图也叫数据流图 ...
- html标记语言 --表单
html标记语言 --表单 七.表单 1.表单标记 1.1表单中的内容 <form></form>定义表单的开始位置和结束位置,表单提交时的内容就是<form>表单 ...
- [转]python 模块 chardet下载及介绍
来源:http://blog.csdn.net/tianzhu123/article/details/8187470/ 在处理字符串时,常常会遇到不知道字符串是何种编码,如果不知道字符串的编码就不 ...