这里占用点地方记录LIst接口的几个常用方法:
List接口:
void add(int index, E element)
boolean remove(Object o);
boolean contains(Object o)
E get(int index)
E set(int index, E element)
直接上代码

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.*;@RunWith(SpringJUnit4ClassRunner.class)  //更改junit的运行期,不要直接去找junit,而是先找spring-test,test模块才能拿到容器
@ContextConfiguration("classpath:applicationContext.xml")  //告知spring的配置文件
public class CollectionTest {@Testpublic void testListRemove0(){List<String> strings = new ArrayList<>();strings.add("科比");strings.add("韦德");strings.add("詹姆士");strings.add("詹姆士");strings.add("詹姆士");strings.add("博士");strings.add("詹姆士");System.out.println(strings);//删除方式0: 使用增强fortry {for (String s : strings){if ("詹姆士".equals(s)){strings.remove(s);}}} catch (Exception e) {e.printStackTrace();} finally {System.out.println(strings); //事实上会报ConcurrentModificationException异常// [科比, 韦德, 詹姆士, 詹姆士, 博士, 詹姆士]}}@Testpublic void testListRemove1(){List<String> strings = new ArrayList<>();strings.add("科比");strings.add("韦德");strings.add("詹姆士");strings.add("詹姆士");strings.add("詹姆士");strings.add("博士");strings.add("詹姆士");System.out.println(strings);//删除方法1:try {Iterator<String> iterator = strings.iterator();while(iterator.hasNext()){String next = iterator.next();if ("詹姆士".equals(next)){ //才不会运行师报空指针异常strings.remove(next);}}} catch (Exception e) {e.printStackTrace();} finally {System.out.println(strings);  //事实上会出现ConcurrentModificationException:并发修改异常,// [科比, 韦德, 詹姆士, 詹姆士, 博士, 詹姆士] , 说明在remove第二个"詹姆士"的时候就会报这个concurrentmodificationException}}@Testpublic void testListRemove2(){List<String> strings = new ArrayList<>();strings.add("科比");strings.add("韦德");strings.add("詹姆士");strings.add("詹姆士");strings.add("詹姆士");strings.add("博士");strings.add("詹姆士");System.out.println(strings);//删除方法2:使用迭代器删除Iterator<String> iterator = strings.iterator();while(iterator.hasNext()){String next = iterator.next();if ("詹姆士".equals(next)){iterator.remove();}}System.out.println(strings); //[科比, 韦德, 博士], 说明删除成功}@Testpublic void testListRemove3(){List<String> strings = new ArrayList<>();strings.add("科比");strings.add("韦德");strings.add("詹姆士");strings.add("詹姆士");strings.add("詹姆士");strings.add("博士");strings.add("詹姆士");System.out.println(strings);//删除方法2:判断之后再删除while(strings.contains("詹姆士")){strings.remove("詹姆士");}System.out.println(strings);//[科比, 韦德, 博士], 说明删除成功}@Testpublic void testListRemove4(){List<String> strings = new ArrayList<>();strings.add("科比");strings.add("韦德");strings.add("詹姆士");strings.add("詹姆士");strings.add("詹姆士");strings.add("博士");strings.add("詹姆士");System.out.println(strings);//删除方法4: 倒循环删除for (int i = strings.size()-1; i >= 0; i--){String s = strings.get(i);if ("詹姆士".equals(s)){strings.remove(s);}}System.out.println(strings);//[科比, 韦德, 博士], 说明删除成功}@Testpublic void testListRemove5(){List<String> strings = new ArrayList<>();strings.add("科比");strings.add("韦德");strings.add("詹姆士");strings.add("詹姆士");strings.add("詹姆士");strings.add("博士");strings.add("詹姆士");System.out.println(strings);//删除方法5: 循环时如果命中目标下标不动for (int i = 0; i < strings.size(); i++){String s = strings.get(i);if ("詹姆士".equals(s)){strings.remove(s);i--;}}System.out.println(strings);//[科比, 韦德, 博士], 说明删除成功}
}

List中删除元素的6种方法比较--前两种就是坑,因为size指针前移。相关推荐

  1. 给自定义控件(Web Control)添加事件的几种方法。前两种方法可以不实现IPostBackEventHandler...

        写自定义控件已经好久了,也有几个用得时间比较长的,但是对于"事件"一直是比较模糊,没有很详细的理解.          最近升级分页控件,由于原来使用的是VB.net(在V ...

  2. 在MFC对话框中显示图片的三种方法(有两种使用OpenCv)

    最近写了一个用对话框显示图片的程序,这里将学习到的东西整理一下: 编程环境:VC6.0+OpenCv1.0 准备工作:用VC6.0生成一个对话框外壳(全被采用默认设置),然后在对话框中添加一个静态控件 ...

  3. List遍历中删除元素

    List遍历主要有索引下标遍历.for循环遍历和Iterator迭代遍历,索引下标和for循环在遍历中删除元素都存在问题,Iterator迭代可以实现遍历中删除元素. 索引下标遍历 List<I ...

  4. php 从数组里删除元素,PHP从数组中删除元素的四种方法实例

    PHP从数组中删除元素的四种方法实例 一.总结 一句话总结:unset(),array_splice(),array_diff(),array_diff_key() 二.PHP从数组中删除元素的四种方 ...

  5. ruby array_在Ruby中使用Array.pop和Array.shift方法从Array中删除元素

    ruby array Ruby Array.pop和Array.shift方法 (Ruby Array.pop and Array.shift methods) If you are reading ...

  6. jquery中删除元素的remove()方法

    今天看jquery中删除元素的方法,包括remove()方法和empty() 方法 remove()方法为删除被选元素及子元素 empty() 方法为删除元素中的子元素 其中remove()方法还有一 ...

  7. 如何在C ++中从容器中删除元素

    How to remove elements from container is a common C++ interview question, so you can earn some brown ...

  8. 从PHP中的数组中删除元素

    有没有一种简单的方法可以使用PHP从数组中删除元素,以便foreach ($array)不再包含该元素? 我认为将其设置为null可以做到,但是显然不起作用. #1楼 如果您有一个数字索引的数组,其中 ...

  9. 【重难点】【Java集合 02】Set、List、Map 的区别、常见的线程安全的集合类、Collection 为什么只能在 Iterator 中删除元素

    [重难点][Java集合 02]List.Set.Map 的区别.常见的线程安全的集合类.Collection 为什么只能在 Iterator 中删除元素 文章目录 [重难点][Java集合 02]L ...

最新文章

  1. 样本方差除以n-1而不是n的原因
  2. 重磅更新!YoloV4最新论文与源码!权重!结构!翻译!
  3. 跨链Cosmos(6)ABCI 原理
  4. Python中如何修改字符串的值
  5. Linux centos7防火墙firewalld相关操作
  6. mysql 用户名中主机$_phpMyAdmin 尝试连接到 MySQL 服务器,但服务器拒绝连接。您应当检查配置文件中的主机、用户名和密码,...
  7. 滑块 组件_组件制作:如何使用链接的输入创建滑块
  8. JDK8新特性(五)之Stream流介绍和获取方式
  9. 深度学习入门(一):LeNet-5教程与详解
  10. [py]flask从0到1-模板/增删改查
  11. 再发Wallop和GMail邀请各4个!
  12. multiprocessing.manager管理的对象需要加锁吗_【极客思考】设计模式:你确定你真的理解了单例模式吗?...
  13. kubernetes视频教程笔记 (21)-存储-configmap
  14. python unpack_关于python中的struct.unpack()
  15. 5个典型实例告诉你:什么是数据可视化
  16. 094 chrome浏览页面常用快捷键
  17. android 全景usb 全景,汽车360度全景USB高清数字信号输出系统的制作方法
  18. 人工智能、核战争与白宫陷落
  19. java 辗转相除_Java实现辗转相除法并求取最大公约数、最小公倍数
  20. Ubuntu16.04+ROS kinetic +Basler_camera环境配置以及相机标定

热门文章

  1. Java循环中的break 和 continue
  2. 基于随机森林和规则抽取框架的防钓鱼浏览器开发
  3. 豆瓣网python_Python实现的豆瓣电影信息查询
  4. 浅谈ANR及如何分析解决ANR(2)
  5. matlab2019支持python_全方位对比:Python、Julia、MATLAB、IDL 和 Java (2019 版)
  6. 真实入手点评:rtx3070和rtx2080ti功耗-tx3070和rtx2080ti参数-rtx3070和rtx2080ti数据对比
  7. 炫龙游戏本Win10系统总是蓝屏崩溃怎么办?
  8. linux批量替换文件名中的日期,rename:Linux 批量修改文件名命令,支持正则表达式...
  9. Notification点击跳转及消失
  10. linux ps faux,35个你会喜欢的Photoshop照片处理动作