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. RxJava的map方法与flatMap方法

    简单讲,map和flatMap都是来完成Observable构造的数据到Observer接收数据的一个转换,这么说有点绕

  2. 一条陌生的出路【过往d心声】

    一条陌生的出路 Vashon的心声 人生就像一列车,车上总有形形色色的人穿梭往来.你也可能会在车上遇到很多你以为有缘分的人,但是车也会有停下来的时候,总会有人从人生这列车上上下下,当你下去的时候你挥挥 ...

  3. http以及http协议简单理解

    HTTP协议是超文本传输协议的缩写,是用于从万维网(WWW)服务器传输超文本到本地浏览器的传送协议:HTTP是一个基于TCP/IP通信协议来传递数据(HTML文件, 图片文件, 查询结果等)HTTP作 ...

  4. 深入解析Web Services

    SOA,面向服务器建构,是一款架构,这几年虽然没前几年那么流行,但是还是有很多企业在用,而Web Services是目前适合做SOA的主要技术之一,通过使用Web Services,应用程序可以对外发 ...

  5. springboot设置接口超时

    springboot 设置接口超时 1.配置文件 application.properties中加了,意思是设置超时时间为20000ms即20s, spring.mvc.async.request-t ...

  6. 【软件构造】第三章第四节 面向对象编程OOP

    第三章第四节 面向对象编程OOP 本节讲学习ADT的具体实现技术:OOP Outline OOP的基本概念 对象 类 接口 抽象类 OOP的不同特征 封装 继承与重写(override) 多态与重载( ...

  7. ORA-03113: end-of-file on & ORA-07445

    --------------ORA-03113: end-of-file on-------------- SQL> show parameter background_dump; NAME T ...

  8. EBS oracle 批量导入更新MOQ(最小拆分量、采购提前期、最小订购量、最小包装量)

    EXCEL的列:组织id,供应商编号,供应商地点,料号,最小拆分量.采购提前期.最小订购量.最小包装量 --采购导入更新MOQ四个值,若有为空的那列,会保留原来的值,不会去更新那列的值 PROCEDU ...

  9. [LOJ] 分块九题 5

    区间开平方,区间查询. lazy标记改为区间是否全是1或者0,这样的区间是没有更新价值的. //Stay foolish,stay hungry,stay young,stay simple #inc ...

  10. linux 部署nginx作为反向代理入口的内核参数/etc/sysctl.conf

    # Kernel sysctl configuration file for Red Hat Linux## For binary values, 0 is disabled, 1 is enable ...