zoukankan      html  css  js  c++  java
  • Arrays.asList返回的List与new ArrayList的区别

        前几天写代码的时候用到将Set转换为List然后继续进行操作,向里面添加元素的时候报错了,代码逻辑类似下面:

    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    
    public class Test {
    
    	public static void main(String[] args) {
    		
    		Set<String> set = new HashSet<String>();
    		set.add("a");
    		set.add("b");
    		set.add("c");
    		
    		//某些判断逻辑
    		
    		List<String> list = Arrays.asList(set.toArray(new String[0]));
    		list.add("d");
    		
    		System.out.println(list);
    	}
    }
    

      代码执行到list.add("d");这一行的时候报错了,报错如下图:

        难道Arrays.asList返回的List与new出来的有什么不同呢!根据报错的信息,是AbstractLst不支持add操作,从源码的结构看List的很多实现类都继承了AbstractList,包括常用的ArrayList。那么Arrays.asList是怎么实现的呢,顺着源码追踪看下去,它的实现非常简单,如下:

    /**
         * Returns a fixed-size list backed by the specified array.  (Changes to
         * the returned list "write through" to the array.)  This method acts
         * as bridge between array-based and collection-based APIs, in
         * combination with {@link Collection#toArray}.  The returned list is
         * serializable and implements {@link RandomAccess}.
         *
         * <p>This method also provides a convenient way to create a fixed-size
         * list initialized to contain several elements:
         * <pre>
         *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
         * </pre>
         *
         * @param a the array by which the list will be backed
         * @return a list view of the specified array
         */
        @SafeVarargs
        public static <T> List<T> asList(T... a) {
            return new ArrayList<>(a);
        }

          注释中倒是说明了返回的是个固定大小的List,确是用ArrayList实现的,当liuyh17211在追踪进ArrayList的时候真相大白了,原来这个ArrayList不是util包中的ArrayList,而只是Arrays类的一个继承了AbstractList内部类,所有代码如下:

    /**
         * @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) {
                if (array==null)
                    throw new NullPointerException();
                a = array;
            }
    
            public int size() {
                return a.length;
            }
    
            public Object[] toArray() {
                return a.clone();
            }
    
            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;
            }
    
            public E get(int index) {
                return a[index];
            }
    
            public E set(int index, E element) {
                E oldValue = a[index];
                a[index] = element;
                return oldValue;
            }
    
            public int indexOf(Object o) {
                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;
            }
    
            public boolean contains(Object o) {
                return indexOf(o) != -1;
            }
        }

        可以发现,这个类确实没有覆盖父类的实现,所以才报错,那还有哪些方法是不支持的呢,在AbstractList中,明确提到了不覆盖就会抛UnsupportedOperationException异常的方法有3个:add(int index, E element),set(int index, E element),remove(int index)。而上面的代码中只覆盖了set方法,可能会调用这几个方法的add(E element),clear(),addAll(int index, Collection<? extends E> c),甚至iterator()方法都没有覆盖,也就是说上面的几个方法都可能在调用中报错,liuyh17211试了一下,只要不去修改list中的值,调用iterator()方法是没问题的。

        由此可见JDK设计的这个返回List,只支持遍历和取值,不能做任何修改,只能作为传递值的桥梁。

    注:我的jdk版本是1.7.0_09,源码不一定和其他版本的jdk下一致。

    老博客地址:http://liuyh17211.iteye.com,新的博客内容暂时会两边同时发表

  • 相关阅读:
    LeetCode 404. 左叶子之和
    三年了
    LeetCode 543. 二叉树的直径
    求结点在二叉排序树中层次的算法
    LeetCode 98. 验证二叉搜索树
    LeetCode 236. 二叉树的最近公共祖先
    LeetCode 129. 求根到叶子节点数字之和
    LeetCode 113. 路径总和 II
    LeetCode 107. 二叉树的层次遍历 II
    LeetCode 144. 二叉树的前序遍历 (非递归)
  • 原文地址:https://www.cnblogs.com/liuyh17211/p/3250365.html
Copyright © 2011-2022 走看看