extends:http://www.cnblogs.com/dolphin0520/p/3933551.html Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()){ Integer integer = iterator.next(); if(integer==2) iterator.remove(); }  …
出现的场景:在迭代器对集合进行遍历的同时,集合本身进行变更操作(add(), remove(), set()). 当正常调用时: import java.util.ArrayList; import java.util.Iterator; public class TestDemo { public static void main(String[] args) { ArrayList<Integer> a = new ArrayList<>(); a.add(1); a.add(…
使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析 在使用Iterator来迭代遍历List的时候如果修改该List对象,则会报java.util.ConcurrentModificationException异常,下面看一个例子演示: 1 package com.others; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.u…
在使用Iterator来迭代遍历List的时候如果修改该List对象,则会报java.util.ConcurrentModificationException异常,下面看一个例子演示: package com.others; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; public cl…
在看ArrayList源码时,看到了一个字段modCount.在add.remove.clear等方法中都有modCount++的操作.不明白什么意思.点进去看了看该字段的解释,总算明白了.modCount是在AbstractList抽象类中定义的.该字段的解释如下所示. /** * The number of times this list has been <i>structurally modified</i>. * Structural modifications are…
1.方法参数的声明语法和catch语句的语法是一样的,你可能会认为主调方法调用一个方法,并向其传递参数,与抛出一个异常传递到catch语句是一样的,是的,有相同之处,但也有更大的不同. 2.主调方法调用一个方法,控制权转移,被调方法执行完,控制权最终还会返回到主调方法.但是,抛出异常到达catch语句,控制权不会再回到抛出端. 3.那么问题来了,抛出异常到达catch语句,控制权不再会到抛出端,这意味着,抛出的异常离开了作用域,自动销毁,那么catch语句还怎么捕获异常对象呢? 解决办法是:建立…
使用Arrays转数组成为List后,不能调用add(...)和remove(...)方法,此时如果调用就会抛出UnsupportedOperationException异常 原因 其实Arrays.asList(...)转成的List不是java.util包下面的ArrayList,而是一个内部静态类ArrayList. asList(T... a)方法 public static <T> List<T> asList(T... a) { return new ArrayList…
第一次用这个方法,结果冒出个莫名其妙的异常来: String[] names = (String[]) mTags.toArray(); 结果会抛出 java.lang.ClassCastException 异常信息 网上谷歌了一下,发现这个 toArray 的正确用法应该是这样子才对: Object[] names = list.toArray(); for (int i = 0; i < names.length; i++) { String name = (String) names[i]…
并发修改异常(ConcurrentModificationException) 这个异常,使用集合的时候应该很常见,这个异常产生的原因是因为java中不允许直接修改集合的结构. 先贴上个有趣的例子,给你们看看: package com.xiongda; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Confirm { public static void mai…
关于Spring的注解其实不难,大致需要以下几个流程: 一.配置Spring的注解支持 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cont…