removeRemove 是什么意思?
remove 时间:2021-08-25 阅读:(
)
list.remove为什么报错
作为Java大家庭中的集合类框架,List应该是平时开发中最常用的,可能有这种需求,当集合中的某些元素符合一定条件时,想要删除这个元素。
如:
public class ListTest {
public static void main(String[] args) {
List<Integer> intList = new ArrayList<Integer>();
Collections.addAll(intList, 1, 2, 3, 5, 6);
// for循环优化写法,只获取一次长度
for(int i = 0, size = intList.size(); i < size; i++) {
Integer value = intList.get(i);
// 符合条件,删除元素
if(value == 3 || value == 5) {
intList.remove(i);
}
}
System.out.println(intList);
}
}
执行后,会抛出IndexOutOfBoundsException,因为集合中存在符合条件的元素,删除后,集合长度动态改变,由于长度只获取一次,发生越界,所以,去掉for循环优化,如:
public class ListTest {
public static void main(String[] args) {
List<Integer> intList = new ArrayList<Integer>();
Collections.addAll(intList, 1, 2, 3, 5, 6);
for(int i = 0; i < intList.size(); i++) {
Integer value = intList.get(i);
// 符合条件,删除元素
if(value == 3 || value == 5) {
intList.remove(i);
}
}
System.out.println(intList);
}
}
输出:[1, 2, 5, 6],漏掉了5这个元素,当i=2的时候,值为3,删除后,后面的元素往前补一位,这时i=3的时候,值为6,跳过了5,这样也不行,随后想到了用for循环增强,不显示的操作下标,直接操作对象,如:
public class ListTest {
public static void main(String[] args) {
List<Integer> intList = new ArrayList<Integer>();
Collections.addAll(intList, 1, 2, 3, 5, 6);
for(Integer value : intList) {
// 符合条件,删除元素
if(value == 3 || value == 5) {
intList.remove(value);
}
}
System.out.println(intList);
}
}
执行后,会抛出ConcurrentModificationException,字面意思是并发修改异常。
异常跟踪信息如下:
Exception inthread "main" java.util.ConcurrentModificationException
atjava.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
at java.util.AbstractList$Itr.next(AbstractList.java:420)
at ListTest.main(ListTest.java:13)
可以大概看出是执行到AbstractList中内部类Itr的checkForComodification方法抛出的异常,至于为什么出现异常,这里可以大概解释一下。
集合遍历是使用Iterator, Iterator是工作在一个独立的线程中,并且拥有一个互斥锁。
Iterator 被创建之后会建立一个指向原来对象的单链索引表,当原来的对象数量发生变化时,这个索引表的内容不会同步改变,所以当索引指针往后移动的时候就找不到要迭代的对象,所以按照 fail-fast原则 Iterator 会马上抛出java.util.ConcurrentModificationException 异常。
所以 Iterator 在工作的时候是不允许被迭代的对象被改变的。
而要解决这个问题,可以使用Iterator的remove方法,该方法会删除当前迭代对象的同时,维护索引的一致性。
如:
public class ListTest {
public static void main(String[] args) {
List<Integer> intList = new ArrayList<Integer>();
Collections.addAll(intList, 1, 2, 3, 5, 6);
Iterator<Integer> it = intList.iterator();
while(it.hasNext()) {
Integer value = it.next();
if(value == 3 || value == 5) {
it.remove();
}
}
System.out.println(intList);
}
}
输出正确结果:[1, 2, 6]。
不使用迭代器的解决方案就是,自己维护索引,删除一个元素后,索引-1,如:
public class ListTest {
public static void main(String[] args) {
List<Integer> intList = new ArrayList<Integer>();
Collections.addAll(intList, 1, 2, 3, 5, 6);
for(int i = 0; i < intList.size(); i++) {
Integer value = intList.get(i);
if(value == 3 || value == 5) {
intList.remove(i);
i--;
}
}
System.out.println(intList);
}
}
输出正确结果:[1, 2, 6]。
还有种取巧的方式是从最后一个元素开始遍历,符合条件的删除,如:
public class ListTest {
public static void main(String[] args) {
List<Integer> intList = new ArrayList<Integer>();
Collections.addAll(intList, 1, 2, 3, 5, 6);
for(int i = intList.size() - 1; i >= 0; i--) {
Integer value = intList.get(i);
if(value == 3 || value == 5) {
intList.remove(i);
}
}
System.out.println(intList);
}
}
输出正确结果:[1, 2, 6]。
最后,Java集合类框架真是大大方便了开发,不用自己去维护数组,随时担心着越界等问题。
当然List的实现类对插入、删除的效率不太一样,这取决于其实现的数据结构,是选择删除,还是选择新建个集合,这里就不做讨论了。
150. SQL语言中,删除表中数据的命令是( )。( ). A.DELETE B.DROP C.CLEAR D.REMOVE
答案是A
delete是删除数据表的记录,语法为delete from <表名> [where<条件>]
drop是删除数据表的字段 ,语法为alter table <表名> drop <字段>
clear是清除主窗口屏幕Remove 是什么意思?
及物动词 1.移走; 排除; 移开; 拿开; 去掉
2.开除; 免除, 解除(职务等)
3.脱去(衣服等);摘下
4. 去除,排除(污渍、不愉快的事物等);使消失
不及物动词 1.迁移; 移居
名词 1.距离;差距;间距
官方网站:点击访问白丝云官网活动方案:一、KVM虚拟化套餐A1核心 512MB内存 10G SSD硬盘 800G流量 2560Mbps带宽159.99一年 26一月套餐B1核心 512MB内存 10G SSD硬盘 2000G流量 2560Mbps带宽299.99一年 52一月套餐...
IT狗为用户提供 在线ping、在线tcping、在线路由追踪、域名被墙检测、域名被污染检测 等实用工具。【工具地址】https://www.itdog.cn/【工具特色】1、目前同类网站中,在线ping 仅支持1次或少量次数的测试,无法客观的展现目标服务器一段时间的网络状况,IT狗Ping工具可持续的进行一段时间的ping测试,并生成更为直观的网络质量柱状图,让用户更容易掌握服务器在各地区、各线...
前些天赵容分享过DogYun(狗云)香港BGP线路AMD 5950X经典低价云服务器的信息(点击查看),刚好账户还有点余额够开个最低配,所以手贱尝试下,这些贴上简单测试信息,方便大家参考。官方网站:www.dogyun.com主机配置我搞的是最低款优惠后14.4元/月的,配置单核,512MB内存,10GB硬盘,300GB/50Mbps月流量。基本信息DogYun的VPS主机管理集成在会员中心,包括...
remove为你推荐
询证函会计事务所的询证函人才培养目标怎样制定人才的培养目标第三方接口支付宝第三方接口如何实现?余额宝收益走势图现在余额宝大概每万份收益是多少?网不易wifi上网有什么优点和缺点色空间求图像处理中颜色空间的介绍,越详细越好零终端如何打开android命令行终端照片ps是什么意思PS照片是什么意思?怎样下载文件百度云网盘只有提取码怎么下文件怎样下载文件电脑上怎么下载安装软件啊
tk域名注册 长沙域名注册公司 私服服务器租用 息壤备案 英语简历模板word iis安装教程 windows2003iso 轻量 爱奇艺vip免费领取 架设邮件服务器 黑科云 深圳主机托管 网站防护 cdn加速 服务器是什么 卡巴斯基免费版 瓦工工资 冰盾ddos防火墙 电脑主机配置 台式机主机 更多