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 ...
随机推荐
- Ubuntu docker 使用命令 系列二
1.下载官方远程仓下的镜像:sudo docker pull <docker 镜像> ,sudo docker pull centos (没有指定版本,就是下载的最新的os) 2. 下载某 ...
- win驱动安装记录
工具:devcon64.exe 安装/更新/删除等记录:c:\windows\inf\setupapi.dev.log
- sublime text3前端开发插件配置以及使用(个人喜爱)
第一步下载软件接着Ctrl +~ (回车)把下面安装包管理添加到sublimeimport urllib.request,os; pf = 'Package Control.sublime-packa ...
- 十分钟搭建App主流框架
搭建主流框架界面 0.达成效果 Snip20150904_5.png 我们玩iPhone应用的时候,有没发现大部分的应用都是上图差不多的结构,下面的TabBar控制器可以切换子控制器,上面又有Navi ...
- Python3基础教程(十九)—— 项目结构
本节阐述了一个完整的 Python 项目结构,你可以使用什么样的目录布局以及怎样发布软件到网络上. 创建Python项目 我们的实验项目名为 factorial,放到 /home/shiyanlou/ ...
- js 数组过滤 filter
let res = this.list.filter(item => routeEqual(this.currentRouteObj, item) || item.name === this.$ ...
- postman使用--批量执行测试用例和数据驱动
批量执行 在我们测试接口的时候,有时候希望执行所有的测试用例,前面讲的都是测试单个的接口,postman提供了我们批量执行接口的功能 点击Runner 然后我们点击run 执行完会统计出我们的结果,失 ...
- 剑指Offer整理笔记
说在前面,本篇的目的是为了学习剑指offer,以及博客园的排版功能,并将文章排版得整洁得体. 梵蒂冈梵蒂冈地方官方
- [LUOGU] P2886 [USACO07NOV]牛继电器Cow Relays
https://www.luogu.org/problemnew/show/P2886 给定无向连通图,求经过k条边,s到t的最短路 Floyd形式的矩阵乘法,同样满足结合律,所以可以进行快速幂. 离 ...
- GIT的简介
本文来自:https://www.breakyizhan.com/git/32.html 这篇GIT教程提供了GIT的基本和高级概念,主要是面向GIT的初学者和专业人士来编写的. 什么是GIT呢? G ...