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. html5获取经纬度,百度api获取街区名,并使用JS保存进cookie

    引用js<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak= ...

  2. Mybatis 开启事务@Transactional

  3. shell中&&和||的使用方法

    测试题: [ -z "" ] && echo 0 || echo 1 的结果是多少 看看这两个 && || 的用户  http://blog.csd ...

  4. ASP.NET MVC 4 RC的JS/CSS打包压缩功能 Scripts.Render和Styles.Render

    打包(Bundling)及压缩(Minification)指的是将多个js文件或css文件打包成单一文件并压缩的做法,如此可减少浏览器需下载多个文件案才能完成网页显示的延迟感,同时通过移除JS/CSS ...

  5. 读javascript高级程序设计02-变量作用域

    一. 延长作用域链 有些语句可以在作用域前端临时增加一个变量对象,该变量对象在代码执行完成后会被移除. ①with语句延长作用域. function buildUrl(){ var qs=" ...

  6. DataTable 导到Excel

    /// <summary> /// 将DataTalbe导出到Excel中 /// </summary> /// <param name="dt"&g ...

  7. oracle给字段添加描述

    oracle中,我们有时候需要给表的字段添加描述.用以下语句即可. alter table a add b varchar2(2); comment on column a.b  is '这是表a的字 ...

  8. hadoop运行原理之shuffle

    hadoop的核心思想是MapReduce,但shuffle又是MapReduce的核心.shuffle的主要工作是从Map结束到Reduce开始之间的过程.首先看下这张图,就能了解shuffle所处 ...

  9. HDU 4939 Stupid Tower Defense (2014 Multi-University Training Contest 7)

    思路:首先红色肯定要放在最后面.前面蓝色和绿色dp求解. dp[i][j]  表示前面(i+j) 个 有 i 个蓝色塔  j个绿色塔 能造成最大伤害. //====================== ...

  10. 二模 (13)day2

    第一题: 题目大意: 给出一个N*M的矩阵,定义一条路径的权值为经过的所有点权值的最大值.求一条从第一行到第N行的路径,使得路径权值最小. N,M<=1000 矩阵内点的权值小于1000. 解题 ...