想把数组转换成List,然后用了Arrays.asList,可是发现这个返回的List中执行操作add与delete中会报
java.lang.UnsupportedOperationException异常。
String [] arrayA = {"123","abc","456"}; for (int i = 0; i < arrayA.length; i++) { System.out.println(arrayA[i]); } System.out.println("------------------------"); List<String> list = Arrays.asList(arrayA); for (String a:list) { System.out.println(a); } System.out.println("------------------------"); System.out.println(list instanceof ArrayList ); System.out.println(list instanceof List ); System.out.println(list.getClass()); list.add("hello"); list.remove(1);
我只想知道为什么会报这样的错误,先看下日志
从上面 我们就意境啊可以知道 这个list 是 arrays中定义的ArrayList,居然还有这一手
下面是java 1.8.0_91 中的源码
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继承了abstractList,而abstractList实现了List接口,在abstractList的设计中,一些没有被子类重写的方法,会爆出UnsupportedOperationException的异常,比如add
/** * 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; }
在add(size(),e)中
原来如此,心头小疑惑。