List remove注意点
public class ListTest { public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> ll = new ArrayList<String>();
ll.add("1");
ll.add("2");
ll.add("3"); for(String str : ll ){
if(str.endsWith("2")){
ll.remove(str);
}
} } } public class ListTest { public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> ll = new ArrayList<String>();
ll.add("1");
ll.add("2");
ll.add("3"); for(String str : ll ){
if(str.endsWith("3")){
ll.remove(str);
}
} } }
public class ListTest { public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> ll = new ArrayList<String>();
ll.add("1");
ll.add("2");
ll.add("3"); for (Iterator it = ll.iterator(); it.hasNext();) {
String str = (String)it.next();
if(str.equals("3")){
it.remove();
System.out.println(it);
}else{
System.out.println(it);
}
} System.out.println("end");
} }
用迭代器就不会有问题:
原因:for是通过指针去判断,如果最后的元素删掉了,那么久没办法判断是否是list结束点了
迭代器的的hasNext()是通过数值判断
public boolean hasNext() {
return cursor != size();
} public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
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 ...
随机推荐
- GUI 测试
图形用户界面( GUI )对软件测试提出了有趣的挑战,因为 GUI 开发环境有可复用的构件,开发用户界面更加省时而且更加精确.同时, GUI 的复杂性也增加了,从而加大了设计和执行测试用例的难度.因为 ...
- Sprint.Net 笔记
有生以来写的第一份博客, 还真不会写, 请高手们指导指导. 1.引入 Spring.Core.dll 和 Common.Logging.dll 两个文 2. 在UI层的Web.conf 的 <C ...
- WinForm窗体更新程序
流程介绍: 打包参阅:WinForm程序打包说明 图一 图二 图三 实现步骤: 主程序 1.检测是否连上ftp服务器 1.1 连接不上,不检测. 1.2 连接上,如果有更新进程, ...
- 向量时钟算法简介——本质类似MVCC
转自:http://blog.chinaunix.net/uid-27105712-id-5612512.html 一.使用背景 先说一下需要用到向量时钟的场景.我们在写数据时候,经常希望数据不要存储 ...
- MYSQL 基于GTID的复制
1.概述 从MYSQL5.6 开始,mysql开始支持GTID复制. 基于日志点复制的缺点: 从那个二进制日志的偏移量进行增量同步,如果指定错误会造成遗漏或者重复,导致数据不一致. 基于GTID复制: ...
- iOS开发UI篇—IOS开发中Xcode的一些使用技巧
iOS开发UI篇—IOS开发中Xcode的一些使用技巧 一.快捷键的使用 经常用到的快捷键如下: 新建 shift + cmd + n 新建项目 cmd + n 新建文 ...
- apache认证、授权、访问控制
认证对象:某一个网站目录. 启用认证 1.即用AllowOverride指令指定哪些指令在针对单个目录的配置文件中有效:AllowOverride AuthConfig 2.设置密码登录访问某个站点或 ...
- python and django
[安装(CentOS 6.5)] 安装python, 采用yum install python,安装后为2.6版本 安装django 1.6.4,再高的版本,就需要python提升版本.下载并解压,然 ...
- [ie兼容]ie7 margin-bottom失效
有时候子元素设置了margin,父元素也设置了margin,父元素的margin在ie7下会失效(包括padding) 解决方法:父容器加属性overflow:hidden;zoom:100%;
- AspectJ的基本使用
参考: https://my.oschina.net/itblog/blog/208067