list如何remove
http://blog.sina.com.cn/s/blog_621b6f0e0100s5n5.html
在java中对list进行操作很频繁,特别是进行list启遍历,这些操作我们都会,也很熟悉,但是对java中list进行删除元素,remove list中的元素就不怎么熟悉了吧,可以说很陌生,是实际操作中也很容易出错,先看看下面这个java中如何remove list 中的元素吧.
- public class test {
- public static void main(String[] args) {
- String str1 = new String("abcde");
- String str2 = new String("abcde");
- String str3 = new String("abcde");
- String str4 = new String("abcde");
- String str5 = new String("abcde");
- List list = new ArrayList();
- list.add(str1);
- list.add(str2);
- list.add(str3);
- list.add(str4);
- list.add(str5);
- System.out.println("list.size()=" + list.size());
- for (int i = 0; i < list.size(); i++) {
- if (((String) list.get(i)).startsWith("abcde")) {
- list.remove(i);
- }
- }
- System.out.println("after remove:list.size()=" + list.size());
- }
- }
大家觉得这个程序打印出来的结果是多少呢?
- 运行结果不是:
- list.size()=5
- after remove:list.size()=0
而是:
- list.size()=5
- after remove:list.size()=2
这是怎么回事呢?到底要如何remove list 中的元素呢?
原因:List每remove掉一个元素以后,后面的元素都会向前移动,此时如果执行i=i+1,则刚刚移过来的元素没有被读取。
怎么解决?有三种方法可以解决这个问题:
1.倒过来遍历list
- for (int i = list.size()-1; i > =0; i--) {
- if (((String) list.get(i)).startsWith("abcde")) {
- list.remove(i);
- }
- }
2.每移除一个元素以后再把i移回来
- for (int i = 0; i < list.size(); i++) {
- if (((String) list.get(i)).startsWith("abcde")) {
- list.remove(i);
- i=i-1;
- }
- }
3.使用iterator.remove()方法删除
- for (Iterator it = list.iterator(); it.hasNext();) {
- String str = (String)it.next();
- if (str.equals("chengang")){
- it.remove();
- }
- }
list如何remove的更多相关文章
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
- [LeetCode] Remove K Digits 去掉K位数字
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- [LeetCode] Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- [LeetCode] Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Element 移除元素
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
随机推荐
- mysql5.7.25集群部署和方案设计(附PXC一键部署脚本)
还记得我们之前部署mysql集群有多麻烦嘛?波哥来救你们啦!~ 我已将项目上传到了我的github仓库中,大家可以点击仓库地址出现的连接登录查看相应的代码!如果觉得不错别忘了转发.点赞哦! 部署步骤: ...
- ios MD5大小写加密
#import "NSString+change.h" #import <CommonCrypto/CommonDigest.h> @implementation NS ...
- ubuntu服务器切换语言
如果在安装Ubuntu Server时选择了中文,在系统安装完毕后,默认是中文,在操作时经常会显示乱码,如果需要设置回英文,则修改/etc/default/locale,将 LANG="cn ...
- 洛谷 P1455 搭配购买
题目描述 明天就是母亲节了,电脑组的小朋友们在忙碌的课业之余挖空心思想着该送什么礼物来表达自己的心意呢?听说在某个网站上有卖云朵的,小朋友们决定一同前往去看看这种神奇的商品,这个店里有n朵云,云朵已经 ...
- Pygame - Python游戏编程入门
>>> import pygame>>> print(pygame.ver)1.9.2a0 如果没有报错,应该是安装好了~ 如果报错找不到模块,很可能是安装版本的问 ...
- Convert Sorted List to Balanced Binary Search Tree leetcode
题目:将非递减有序的链表转化为平衡二叉查找树! 参考的博客:http://blog.csdn.net/worldwindjp/article/details/39722643 利用递归思想:首先找到链 ...
- AspNetCore容器化(Docker)部署(一) —— 入门
一.docker注册安装 Windows Docker Desktop https://www.docker.com/products/docker-desktop Linux Docker CE h ...
- Eclipse启动的时候提示:Failed to load JavaHL Library
版本信息: Eclipse Project Release Notes Release 4.7.3 启动提示: Subclipse talks to Subversion via a Java API ...
- m3u8 格式转MP4
现在很多视频网站采用HLS流媒体的方式来提供视频直播,在HTML源代码中flash的播放地址为 http://xxxxxx/video/movie.m3u8 1.m3u8下载的格式大致如下: #EXT ...
- 倍增实现LCA
Today,we will talk about how to find The Least Common Ancestor. Now ,let us get into the business(正题 ...