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. Error: listen EADDRINUSE

    有可能是已经作用了18001端口 netstat -antp|grep 18001 kill 然后重启程序. events.js:72 throw er; // Unhandled 'error' e ...

  2. 【apache】yum 安装Apache(Centos 6.5)

    一.首先在系统上面查询一下是否已经安装了apache [Apache在linux系统里的名字是httpd] rpm    -qa    httpd 如果有返回的信息,则会显示已经安装的软件.如果没有则 ...

  3. DES MAC PIN HEX

    /* void DesEncrypt( UCHAR * auchInput,UCHAR * auchKey,UCHAR * auchOutput=NULL); Function: DesEncrypt ...

  4. bootstrap之消息提示

    <!DOCTYPE html><html>   <head>      <title>Bootstrap</title>      < ...

  5. HDU 1106 排序 题解

    排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  6. Zabbix点滴

    [ZABBIX需试验的项] 1. 手工设置ITEM, 采用descr为依据值,用SNMP取流量 2. 通过aggregate item类型,设置取虚拟机数量的值(描绘出虚拟机的增长与下降曲线) [20 ...

  7. bzoj 3124: [Sdoi2013]直径

    #include<cstdio> #include<iostream> #define M 400009 #define ll long long using namespac ...

  8. 我的qq邮箱的GPG公钥

    呵呵,赶一波潮流,我的邮箱 175420840@qq.com 的GPG公钥如下,也可以在这里直接下载.具体可参见阮一峰的<GPG入门教程>. -----BEGIN PGP PUBLIC K ...

  9. 2014年4月份第3周51Aspx源码发布详情

    WPY净水机网站源码  2014-4-14 [VS2008]源码描述: 实现产品展示,在线留言,信息发布,在线咨询,营销网络地图. 网站基本管理:网站banner管理 管理首页滚动图片信息 网站右下部 ...

  10. easyui datagrid

    function initData(){ //数据列表 yftjsy=$("#yftjsy").datagrid({ url: '', fit:true, showFooter:f ...