1、异常代码:
public static void main(String[] args) {
String[] arr = new String[]{"a", "b", "c"};
//在使用Arrays.asList转化数组成为list的时候,如果做添加操作,不能直接Arrays.asList()
List<String> list = Arrays.asList(arr);
list.add("e"); //添加元素"e", 报java.lang.UnsupportedOperationException
}
2、异常信息:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
3、异常分析:
在使用 Arrays.asList()转化数组成为list的时候,生成了ArrayList, 表面上看是 java.util.ArrayList,实际上是Arrays的内部类ArrayList, 两个ArrayList,都是继承 AbstractList,这他妈就是坑,不进去看源码仔细了解,还以为是一个ArrayList,但是进去后才发现,Arrays的内部类ArrayList没有重写 AbstractList的add和remove方法,再去看AbstractList的add和remove方法,居然是直接抛出异常 java.lang.UnsupportedOperationException,没有任何处理。这就导致上面这种方法生成的Arrays的ArrayList使用add或者remove方法会直接抛出异常。
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
/**
* Sole constructor. (For invocation by subclass constructors, typically
* implicit.)
*/
protected AbstractList() {
}
/**
* Appends the specified element to the end of this list (optional
* operation).
*
* <p>Lists that support this operation may place limitations on what
* elements may be added to this list. In particular, some
* lists will refuse to add null elements, and others will impose
* restrictions on the type of elements that may be added. List
* classes should clearly specify in their documentation any restrictions
* on what elements may be added.
*
* <p>This implementation calls {@code add(size(), e)}.
*
* <p>Note that this implementation throws an
* {@code UnsupportedOperationException} unless
* {@link #add(int, Object) add(int, E)} is overridden.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
* @throws UnsupportedOperationException if the {@code add} operation
* is not supported by this list
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this list
* @throws NullPointerException if the specified element is null and this
* list does not permit null elements
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this list
*/
public boolean add(E e) {
add(size(), e);
return true;
}
/**
* {@inheritDoc}
*
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
abstract public E get(int index);
/**
* {@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 E set(int index, E element) {
throw new 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();
}
........
........
........
}
4、解决方法:
在使用Arrays.asList()做转化的时候,如果要进行修改操作,需要再转化一次。
public static void main(String[] args) {
String[] arr = new String[]{"a", "b", "c"};
//在使用Arrays.asList转化数组成为list的时候,如果做添加操作,不能这样用
//List<String> list = Arrays.asList(arr);
//list.add("e"); //添加元素"e", 报java.lang.UnsupportedOperationException
//在使用Arrays.asList()做转化的时候,如果要进行修改操作,需要再转化一次。
List<String> list = new ArrayList<>(Arrays.asList(arr));
list.add("e");
System.out.println(list);
}
使用new ArrayList<>这样再转化为 java.util.ArrayList ,就可以进行修改操作了。