问题一:
查看下面方法的运行结果:
public static void main(final String[] args) {System.out.println(Arrays.asList(new String[] { "a", "b", "c" }));System.out.println(Arrays.asList(new Integer[] { 1, 2, 3, 4, 5 }));System.out.println(Arrays.asList(new int[] { 1, 2, 3 }));System.out.println(Arrays.asList(new String[] { "a", "b" }, "c", "d", "e", "f"));}
输出结果:
- Arrays.asList(new String[] { "a", "b", "c" }) 正常输出.
- Arrays.asList(new Integer[] { 1, 2, 3, 4, 5 }) 正常输出.
- Arrays.asList(new int[] { 1, 2, 3 }) 输出一串对象地址值.
- Arrays.asList(new String[] { "a", "b" }, "c", "d", "e", "f") 输出对象地址值和其它字符串.
源码分析
public static <T> List<T> asList(T... a) {return new ArrayList<>(a);}
从asList源码可以看出,该方法接受参数为可变参数列表,并且参数类型为泛型,而泛型只能只能代表引用类型,不能是原始类型,在第三行输出中,int是基本类型,int[] 是引用类型,所以会把整个int[] 数组作为一个参数传递到集合中去.因此打印的是数组的地址值.
问题二:
查看下面方法的运行结果:
public static void main(final String[] args) {List<Integer> list = Arrays.asList(new Integer[] { 1, 2, 3, 4, 5 });System.out.println(list);list.add(6);
}
输出结果:
正常输出,但是添加报错.
源码分析
public static <T> List<T> asList(T... a) {return new ArrayList<>(a);}
asList方法会创建一个新的集合,但是呢,这个集合不是java.util.ArrayList这个类,而是Arrays方法中的一个内部类,这个类型继续了AbstractList的默认实现,如下图;
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);}@Overridepublic int size() {return a.length;}@Overridepublic 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;}@Overridepublic E get(int index) {return a[index];}@Overridepublic E set(int index, E element) {E oldValue = a[index];a[index] = element;return oldValue;}@Overridepublic 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;}@Overridepublic boolean contains(Object o) {return indexOf(o) != -1;}@Overridepublic Spliterator<E> spliterator() {return Spliterators.spliterator(a, Spliterator.ORDERED);}@Overridepublic void forEach(Consumer<? super E> action) {Objects.requireNonNull(action);for (E e : a) {action.accept(e);}}@Overridepublic 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]);}}@Overridepublic void sort(Comparator<? super E> c) {Arrays.sort(a, c);}}
查看AbstractList中add方法的默认实现:该add方法直接抛出一个不支持的操作异常.
public boolean add(E e) {add(size(), e);return true;
}public void add(int index, E element) {throw new UnsupportedOperationException();
}
其实不只是add方法,其他方法: remove, set 方法都会抛出该异常
/*** {@inheritDoc}** <p>This implementation always throws an* {@code UnsupportedOperationException}.** @throws UnsupportedOperationException {@inheritDoc}* @throws IndexOutOfBoundsException {@inheritDoc}*/public E remove(int index) {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 E set(int index, E element) {throw new UnsupportedOperationException();}