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 ...
随机推荐
- .net4.5注册到iis
开始->所有程序->附件->鼠标右键点击“命令提示符”->以管理员身份运行->%windir%\Microsoft.NET\Framework\v4.0.30319\as ...
- Quartz使用二 通过属性传递数据
上一篇介绍了通过context.getJobDetail().getJobDataMap()方式获取传递的数据,其实可以通过定义属性来传递参数 package org.tonny.quartz; im ...
- CENTOS6.4上KVM虚拟机环境搭建
CENTOS6.4上KVM虚拟机环境搭建 关键词: KVM,虚拟机,windows7, VNC, 桥接网络,br0, SCSI, IDE 环境: host: CENTOS6.4 guest: ...
- Hadoop分布式集群安装
环境准备 操作系统使用ubuntu-16.04.2 64位 JDK使用jdk1.8 Hadoop使用Hadoop 2.8版本 镜像下载 操作系统 操作系统使用ubun ...
- 最简单的教程:在Ubuntu操作系统里安装Docker
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有任何 ...
- idea集成 MyBatis Generator 插件,自动生成dao,model,sql map文件
1.集成到开发环境中 以maven管理的功能来举例,只需要将插件添加到pom.xml文件中即可.(注意此处是以plugin的方式,放在<plugins></plugins>中间 ...
- 项目:开发->测试->上线:流程规范
1) 项目分析 2) 项目经理分工协作 程序就具体流程: 1: 按项目名称: 建git, 数据库, 线上测试虚拟机 2: 按项目经理分配的大的模块. 自行划分工作阶段 a: 划分为小的模块 b: 预估 ...
- JSTL标签判断list是否为空
jsp页面判断获得action传的list的是否为空或者list.size的长度,就可以用fn这个标签: <c:if test="${list== null || fn:length( ...
- SpringMVC中的几种事务管理器
转载https://blog.csdn.net/qq_26222859/article/details/52032853 1JDBC及iBATIS.MyBatis框架事务管理器 <bean id ...
- ajax 为 select 赋值
html 页面<tr> <td class="dc-form-left">权限组:</td> <td class="dc-for ...