正确在遍历中删除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 ...
随机推荐
- javascript学习(1)用户的Javascript 放在哪里和函数的绑定方式
一.实验 1:js脚本放在那里最合适? 1.代码 1.1.test.html <!DOCTYPE html><html> <head> < ...
- guava-19.0和google-collections-1.0 的 ImmutableSet 类冲突
guava-19.0 google-collections-1.0 都有 ImmutableSet 类,包路径也一致,前者有 copyOf(Collection)? 一.应用报错: 二.解决办法 co ...
- 32位centos6.5 mysql rpm包下载
查看centos版本号和位数: http://www.cnblogs.com/grey-wolf/p/7472507.html mysql下载: 1.进入https://dev.mysql.com/d ...
- Spring 4.2.5 + Quartz 2.2.0整合
jar包使用的Maven库管理的,在这就不罗列了,注意下只有spring3.x以上的版本才支持quartz2.x的版本. 配置文件: <?xml version="1.0" ...
- vue2与vue1的区别
在前面的学习过程中我们已经对vue1有了一定的了解,下面我们来学习一下vue2,看一下vue1与vue2有什么区别. 区别1: 在vue2中使用v-for指令时它可以添加重复的内容,就像可以添加相同的 ...
- 开源博客系统使用springmvc
https://github.com/Zephery/newblog http://www.wenzhihuai.com/index.html
- flask开发用户管理系统wtf版
#coding=utf-8 from flask import Flask from flask import request from flask import redirect from flas ...
- Python3NumPy——数组(1)之创建
开篇 numpy库作为科学计算的基础库,其地位相当重要,它是对数组操作的基石.它的存在使得线性代数以及矩阵论等相关知识在计算机上的表达更加方便与简单,集中体现出了人想办法,计算机去工作. Python ...
- win10被微软流氓更新后编译基于visual Studio的web项目报[ArgumentOutOfRangeException: 指定的参数已超出有效值的范围
最近忙得算焦头烂额.就在这个时候.一个不留神.微软的自动更新打开了.这流氓就在我百忙之中强迫我休息了一个多小时. 焦急等待它更新完以后赶紧打开visual studio跑代码.运行好几次都报错.想想不 ...
- [LeetCode] Print Binary Tree 打印二叉树
Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...