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注意点的更多相关文章

  1. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  2. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  3. [LeetCode] Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  4. [LeetCode] Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  5. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  6. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  7. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  8. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  9. [LeetCode] Remove Element 移除元素

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  10. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

随机推荐

  1. js中面向对象

    1.对象的表示方法,以下是对象的两种方法:第二种方法是使用函数构造器来创建一个对象. 2.对象的一种表达方式,这种方式更像Java中对象的创建,就是用一个new来创建一个对象实例.面向对象的封装.样式 ...

  2. SpringMVC -- 梗概--贰

    1.为什么要配置: mvc:annotation-driven 1>在springMVC的处理流程中,有两个重要组件:HandlerMapping和HandlerAdapter 分别负责解析Ha ...

  3. 51nod 1135 原根

    题目链接:51nod 1135 原根 设 m 是正整数,a是整数,若a模m的阶等于φ(m),则称 a 为 模m的一个原根.(其中φ(m)表示m的欧拉函数) 阶:gcd(a,m)=1,使得成立的最小的 ...

  4. css布局左右技巧分享

    无意之间发现左右侧布局很多技巧在里边,接下来分享下实例: <div style="width:40px;height:36px;float:left;overflow:hidden; ...

  5. android代码嵌入html代码

    有时候需要在一个TextView控件中设置两种不同颜色的字体,这时候可以使用Html.fromHtml方法实现 例如: tvTaskDesc.setText(Html.fromHtml("当 ...

  6. hdu 4639 Hehe

    http://acm.hdu.edu.cn/showproblem.php?pid=4639 每一段 "hehe..... " 相互独立  将每一段 "hehe..... ...

  7. Phonebook 导出联系人到SD卡(.vcf)

    2014-01-13 16:53:55 1. 在Phonebook中导出联系人到内部存储,SD卡或者通过蓝牙.彩信.邮件等分享联系人时,通常会先将选择的联系人打包生成.vcf文件,然后将.vcf文件分 ...

  8. MyEclipse中Maven的配置

    之前在MyEclipse这个IDE中配置Maven,完成配置后启动Maven时出现-Dmaven.multiModuleProjectDirectory system propery is not s ...

  9. JBoss集群中启用HTTPS协议

    Generate server certificate Note: If you already have certificate created then this section can be i ...

  10. 记录一些容易忘记的属性 -- UITabBarController

    UIViewController中的  @property(nonatomic,copy) NSString *title;  // Localized title for use by a pare ...