http://blog.sina.com.cn/s/blog_621b6f0e0100s5n5.html

在java中对list进行操作很频繁,特别是进行list启遍历,这些操作我们都会,也很熟悉,但是对java中list进行删除元素,remove list中的元素就不怎么熟悉了吧,可以说很陌生,是实际操作中也很容易出错,先看看下面这个java中如何remove list 中的元素吧.

  1. public class test {
  2. public static void main(String[] args) {
  3. String str1 = new String("abcde");
  4. String str2 = new String("abcde");
  5. String str3 = new String("abcde");
  6. String str4 = new String("abcde");
  7. String str5 = new String("abcde");
  8. List list = new ArrayList();
  9. list.add(str1);
  10. list.add(str2);
  11. list.add(str3);
  12. list.add(str4);
  13. list.add(str5);
  14. System.out.println("list.size()=" + list.size());
  15. for (int i = 0; i < list.size(); i++) {
  16. if (((String) list.get(i)).startsWith("abcde")) {
  17. list.remove(i);
  18. }
  19. }
  20. System.out.println("after remove:list.size()=" + list.size());
  21. }
  22. }

大家觉得这个程序打印出来的结果是多少呢?

Java代码
  1. 运行结果不是:
  2. list.size()=5
  3. after remove:list.size()=0

而是:

Java代码
  1. list.size()=5
  2. after remove:list.size()=2

这是怎么回事呢?到底要如何remove list 中的元素呢?

原因:List每remove掉一个元素以后,后面的元素都会向前移动,此时如果执行i=i+1,则刚刚移过来的元素没有被读取。

怎么解决?有三种方法可以解决这个问题:

1.倒过来遍历list

Java代码
  1. for (int i = list.size()-1; i > =0; i--) {
  2.   if (((String) list.get(i)).startsWith("abcde")) {
  3.    list.remove(i);
  4.   }
  5. }

2.每移除一个元素以后再把i移回来

Java代码
  1. for (int i = 0; i < list.size(); i++) {
  2.   if (((String) list.get(i)).startsWith("abcde")) {
  3.    list.remove(i);
  4.    i=i-1;
  5.   }
  6. }

3.使用iterator.remove()方法删除

Java代码
    1. for (Iterator it = list.iterator(); it.hasNext();) {
    2.   String str = (String)it.next();
    3.   if (str.equals("chengang")){
    4.    it.remove();
    5.   }
    6. }

list如何remove的更多相关文章

  1. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  2. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  3. [LeetCode] Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  4. [LeetCode] Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  5. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  6. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  7. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  8. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  9. [LeetCode] Remove Element 移除元素

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  10. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

随机推荐

  1. 嵌入式ARM开发板学习方法步骤

    嵌入式开发就是指在嵌入式操作系统下进行开发,一般常用的系统有linux,android. 平台:Cortex-A9开发板 嵌入式技术学习如何入手,从何学起呢, 以下内容简单介绍嵌入式开发的学习步骤及如 ...

  2. ElasticSearch可视化工具 Kibana

    Kibana要和ElasticSearch 版本一致,默认的端口号是:5601

  3. Python3简明教程(十一)—— 类

    本节中将通过定义一些简单的 Python 类,来学习 Python 面向对象编程的基本概念. 定义类 在写你的第一个类之前,你应该知道它的语法.我们以下面这种方式定义类: class nameofth ...

  4. golang结构体排序 - 根据下载时间重命名本地文件

    喜M拉Y下载音频到手机,使用ximalaya.exe 解密[.x2m]为[.m4a]根据文件下载创建时间,顺序重命名文件,方便后续播放. 源码如下:package main import ( &quo ...

  5. Bug的定义和分类

    什么是BUG 使用人工或自动手段,来运行或测试某个系统的过程.其目的在于检验它是否满足规定的需求或弄清预期结果与实际结果之间的差别 BUG分类 完全没有实现的功能 基本实现了用户需要的功能,但是运行时 ...

  6. Gradle dependencies 依赖方式

    implementation:使用了该命令编译的依赖,仅仅对当前的Moudle提供接口 依赖首先应该设置为implement的,如果没有错,那就用implement,如果有错,那么使用api指令 那为 ...

  7. JavaScript设计模式基础之面向对象的JavaScript(二)

    多态 多态的实际含义:同一操作作用与不同的对象上面,可以产生不同的解释和不同的执行结果,就是说,给不同的对象发送同一个消息 的时候,这些对象会根据这个消息分别给出不同的反馈 代码如下: class D ...

  8. 题解 洛谷P3622/BZOJ1151【[APIO2007]动物园】

    这一道题,我也是搞了很久才搞懂的(也就两个多小时). 感谢Rayment大佬的题解! 我们进入正题. 对于一个笼子里的动物,我们可以选择撤走或不撤走,可以用0和1来表示,很容易就想到二进制,想到状压d ...

  9. 关于inet_ntop、inet_pton中的n和p分别代表的意义

    函数名中的p和n非别代表表达(presentation)和数值(numeric).地址的表达格式通常是ASCII字符串,数值格式则是存放到套接字地址结构中的二进制值. 参考自:https://blog ...

  10. ArrayList集合(JDK1.8)

    简述 List是继承于Collection接口,除了Collection通用的方法以外,扩展了部分只属于List的方法. 常用子类  ?ArrayList介绍 1.数据结构 其底层的数据结构是数组,数 ...