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 ...
随机推荐
- flutter基础
1.flutter安装 1.参考官网安装sdk https://flutter.io/get-started/install 安卓和IOS需要分别配置对应的开发环境,安卓建议使用as开发,安装Flut ...
- spring @value 为什么没有获取到值
1.配置文件的路径没有扫描到 2.注解的bean 不是通过spring托管的.bean 要通过spring 注解,引用的时候要用@Autowired 自动注入的bean 不要用new 出来的bean ...
- 一个PHP开发APP接口的视频教程
感觉php做接口方面的教程很少,无意中搜到了这个视频教程,希望能给一些人带来帮助http://www.imooc.com/learn/163
- Python3简明教程(九)—— 文件处理
文件是保存在计算机存储设备上的一些信息或数据.你已经知道了一些不同的文件类型,比如你的音乐文件,视频文件,文本文件.Linux 有一个思想是“一切皆文件”,这在实验最后的 lscpu 的实现中得到了体 ...
- window.onload和DOMContentLoaded的区别
一.何时触发这两个事件? 1.当 onload 事件触发时,页面上所有的DOM,样式表,脚本,图片,flash都已经加载完成了. 2.当 DOMContentLoaded 事件触发时,仅当DOM加载完 ...
- NetBeans 默认编码修改方法
如果要NetBeans用UTF-8对文件进行解码,需要修改配置文件,具体方法如下: 1. 找到你的Netbeans安装目录下的etc文件夹,如D:\Program Files\NetBeans 8.2 ...
- 区间DP || HDU 6249 Alice’s Stamps
题意:标号为1-n的n种邮票,m个邮票集,每个集里有标号从Li到Ri的邮票,要从中选K个邮票集,使这K个邮票集能覆盖最多种的邮票,问最多能覆盖多少种邮票 思路:区间DP (我:??? f[i][j]表 ...
- 第1节 flume:7、flume的监控文件夹,实现数据收集到hdfs上
1.2.2 采集案例 1.采集目录到HDFS 需求分析 结构示意图: 采集需求:某服务器的某特定目录下,会不断产生新的文件,每当有新文件出现,就需要把文件采集到HDFS中去 根据需求,首先定义以下3大 ...
- ASP.NetCore 错误 NU1605 检测到包降级: Microsoft.Data.Sqlite 从 2.2.1 降级到 2.1.0
找到使用的.csproj文件 将 <PackageReference Include="Microsoft.Data.Sqlite" Version="2.1.0& ...
- 5.1 qbxt 一测 T1
禁咒检验 (3MB / 2s)[问题描述] 在古老的世界里,有一个神奇的职业叫做魔法师. 魔法师的特点是会魔法,施放魔法需要念咒语. 在古老的世界里,有一个神奇的职业叫做码农.码农的工作是帮助魔法师记 ...