List在修改时报java.util.ConcurrentModificationException的原因和解决方法已经很常见了,一般面试时也会问到,可以参考这篇文章:http://www.cnblogs.com/liuling/p/2013-8-21-04.html,但是今天遇到了另一种异常UnsupportedOperationException,代码如下:
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5);
Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()){
Integer integer = iterator.next();
if(integer % 2 == 0){
iterator.remove();
}
}
for(Integer i : list){
System.out.println(i);
}
}
看样子是返回的list不支持修改的操作,根据调试结果,实际上程序是运行到了 AbstractList<E>中的remove方法中,而可以看到源码中该类下边的几个修改(新增、删除等)方法都是抛出 UnsupportedOperationException的,代码如下:
/**
* {@inheritDoc}
*
* <p>This implementation always throws an
* {@code UnsupportedOperationException}.
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
/**
* {@inheritDoc}
*
* <p>This implementation always throws an
* {@code UnsupportedOperationException}.
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E remove(int index) {
throw new UnsupportedOperationException();
}
这是为什么呢?因为我习惯性以为Arrays.asList返回的是一个java.util.ArrayList但实际上并不是,一下是源码:
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
/**
* @serial include
*/
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
private static final long serialVersionUID = -2764017481108945198L;
private final E[] a;
ArrayList(E[] array) {
a = Objects.requireNonNull(array);
}
@Override
public int size() {
return a.length;
}
@Override
public Object[] toArray() {
return a.clone();
}
@Override
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
int size = size();
if (a.length < size)
return Arrays.copyOf(this.a, size,
(Class<? extends T[]>) a.getClass());
System.arraycopy(this.a, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
@Override
public E get(int index) {
return a[index];
}
@Override
public E set(int index, E element) {
E oldValue = a[index];
a[index] = element;
return oldValue;
}
@Override
public int indexOf(Object o) {
E[] a = this.a;
if (o == null) {
for (int i = 0; i < a.length; i++)
if (a[i] == null)
return i;
} else {
for (int i = 0; i < a.length; i++)
if (o.equals(a[i]))
return i;
}
return -1;
}
@Override
public boolean contains(Object o) {
return indexOf(o) != -1;
}
@Override
public Spliterator<E> spliterator() {
return Spliterators.spliterator(a, Spliterator.ORDERED);
}
@Override
public void forEach(Consumer<? super E> action) {
Objects.requireNonNull(action);
for (E e : a) {
action.accept(e);
}
}
@Override
public void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);
E[] a = this.a;
for (int i = 0; i < a.length; i++) {
a[i] = operator.apply(a[i]);
}
}
@Override
public void sort(Comparator<? super E> c) {
Arrays.sort(a, c);
}
}
可以看到返回的实际上是一个静态内部类ArrayList<E>,而该类虽然继承了AbstractList,但是并没有Override抽象类中关于新增、删除的方法,因此才会抛出UnsupportedOperationException异常。
解决方法很简单,把返回的List包装一下就行:
List<Integer> list = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
参考:http://blog.csdn.net/liuzhigang1237/article/details/7197074