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. mysql5.7.25集群部署和方案设计(附PXC一键部署脚本)

    还记得我们之前部署mysql集群有多麻烦嘛?波哥来救你们啦!~ 我已将项目上传到了我的github仓库中,大家可以点击仓库地址出现的连接登录查看相应的代码!如果觉得不错别忘了转发.点赞哦! 部署步骤: ...

  2. ios MD5大小写加密

    #import "NSString+change.h" #import <CommonCrypto/CommonDigest.h> @implementation NS ...

  3. ubuntu服务器切换语言

    如果在安装Ubuntu Server时选择了中文,在系统安装完毕后,默认是中文,在操作时经常会显示乱码,如果需要设置回英文,则修改/etc/default/locale,将 LANG="cn ...

  4. 洛谷 P1455 搭配购买

    题目描述 明天就是母亲节了,电脑组的小朋友们在忙碌的课业之余挖空心思想着该送什么礼物来表达自己的心意呢?听说在某个网站上有卖云朵的,小朋友们决定一同前往去看看这种神奇的商品,这个店里有n朵云,云朵已经 ...

  5. Pygame - Python游戏编程入门

    >>> import pygame>>> print(pygame.ver)1.9.2a0 如果没有报错,应该是安装好了~ 如果报错找不到模块,很可能是安装版本的问 ...

  6. Convert Sorted List to Balanced Binary Search Tree leetcode

    题目:将非递减有序的链表转化为平衡二叉查找树! 参考的博客:http://blog.csdn.net/worldwindjp/article/details/39722643 利用递归思想:首先找到链 ...

  7. AspNetCore容器化(Docker)部署(一) —— 入门

    一.docker注册安装 Windows Docker Desktop https://www.docker.com/products/docker-desktop Linux Docker CE h ...

  8. Eclipse启动的时候提示:Failed to load JavaHL Library

    版本信息: Eclipse Project Release Notes Release 4.7.3 启动提示: Subclipse talks to Subversion via a Java API ...

  9. m3u8 格式转MP4

    现在很多视频网站采用HLS流媒体的方式来提供视频直播,在HTML源代码中flash的播放地址为 http://xxxxxx/video/movie.m3u8 1.m3u8下载的格式大致如下: #EXT ...

  10. 倍增实现LCA

    Today,we will talk about how to find The Least Common Ancestor. Now ,let us get into the business(正题 ...