记一次奇葩问题-Arrays.asList()和 new ArrayList<>()的区别

代码如下:

public static void main(String[] args) {
        Integer[] intArr={9,4,7,8,4,6};
        List<Integer> intOldList = Arrays.asList(intArr);
        List<Integer> intNewList = new ArrayList<>();
        intNewList.add(9);
        System.out.println(intOldList.toArray().getClass().getSimpleName());
        System.out.println(intNewList.toArray().getClass().getSimpleName());
        Integer [] toArrayOld = (Integer[]) intOldList.toArray();
        //这一行 类型转换异常
        Integer [] toArrayNew = (Integer[]) intNewList.toArray();
    }

打印结果:

Integer[]
Object[]
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
    at test.main(test.java:13)

两个集合类,一个是通过数组工具类的Arrays.asList转换而来的,一个是直接new出来的。打印出来的结果却不一样,一个是Integer[],一个是Object[]。最后第二个直接new出来的在类型转换时还报了异常。

原因如下:

Arrays.asList返回的是Arrays中的内部类ArrayList

/**
     * @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);
        }
    }

但是直接new出来直接就是ArrayList,两者是不一样的,具体哪里不一样还在研究。

 

记一次奇葩问题-Arrays.asList()和 new ArrayList<>()的区别

上一篇:25.判断带头结点的循环双链表是否对称


下一篇:贪心——55. 跳跃游戏