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. 如何配置TomCat

    1.先查看你自己java的jdk的版本号 2.通过jdk版本号确定下载的Tomcat版本 ,因为我的是jdk 1.8的,所以要下载Tomcat 8版本 附上下载官网http://tomcat.apac ...

  2. Excel数据导入SQL Server

    基本有2种方案,都是无需安装Office的方案 Ole DB读取 + BulkCopy 获取Excel各个SheetName //连接串 string strConn = "Provider ...

  3. ubuntu下安装memcache及memcached

    memcache 和 memcached 有什么区别呢? memcache最早是在2004年2月开发的,而memcached最早是在2009年1月开发的.所以memcache的历史比memcached ...

  4. IE11/Flash页游白屏怎么办!立刻开启IE大地址模式!缓解浏览器白屏问题

    您是否经常发现IE白屏了,具体表现为点开新网页时无法显示,只能切换标签,用任务管理器一看,内存1.2G之多. 这是因为IE11可能有内存泄露问题,内存不断增长以至于无法申请新的内存,于是IE就完蛋了! ...

  5. 响应式布局(CSS3弹性盒flex布局模型)

    传统的布局方式都是基于盒模型的 利用display.position.float来布局有一定局限性 比如说实现自适应垂直居中 随着响应式布局的流行,CSS3引入了更加灵活的弹性布局模型 flex弹性布 ...

  6. bzoj3307 雨天的尾巴 题解(线段树合并+树上差分)

    Description N个点,形成一个树状结构.有M次发放,每次选择两个点x,y 对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成 所有发放后,每个点存放最多的是哪种物品. Input ...

  7. Ioc 之 Unity的AOP功能

    前面我们介绍了Unity的依赖注入功能,现在来介绍下Unity的AOP功能.AOP是面向切面编程,它能够使我们在不改变现有代码结构的情况下额外的为其添加一些功能. 我们还是使用配置文件来对类型进行注入 ...

  8. 【转】C# WinForm中的Label如何换行

    第一种是把Label的AutoSize属性设为False,手动修改Label的大小.这样的好处是会因内容的长度而自动换行,但是当内容的长度超过所设定的大小时,多出的内容就会无法显示.因此,这种方法适合 ...

  9. 小程序08 小程序访问服务器API

    后台交互 小程序是前端框架,需要和后台交互,本次课程主要介绍网络API. 小程序提供的网络访问API wx.request接口 发起 HTTPS 网络请求. 使用rqeust接口前的工作 1.小程序需 ...

  10. python3 yum not found

    vi /urs/bin/yum 将#!/usr/bin/python的python改为python2.x(你系统的python2的版本)