java.util.ArrayList与java.util.Arrays$ArrayList区别(转)

转自:http://blog.csdn.net/maywehe/article/details/52553954

写demo的时候,为了避免用list.add方法,特意写了个数组然后转换成list。一开始以为转换成的list就是实现了AbstractList的通用的List, 比如ArrayList或者LinkedList等。 当调用add方法的时候, 奇怪的事情发生了。

String[] arrays = new String[] { "1", "2", "3" };
List<String> list = Arrays.asList(arrays);
list.add("4");
   
   
  • 1
  • 2
  • 3

抛异常:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:148)
    at java.util.AbstractList.add(AbstractList.java:108)
    at com.maywe.list.Demo.main(Demo.java:43)
   
   
  • 1
  • 2
  • 3
  • 4

List 调用add方法是最普遍不过的场景,怎么会抛异常呢? 赶快去研究下源码。

java.util.Arrays$ArrayList 源码

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;
        }
    }

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

的确是实现了AbstractList,但是没有实现add方法, 在看看AbstractList的add方法:

public boolean add(E e) {
        add(size(), e);
        return true;
    }

public void add(int index, E element) {
        throw new UnsupportedOperationException();
    }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

UnsupportedOperationException 就是怎么抛出来的。

所以java.util.Arrays$ArrayList只能在不超过capacity的情况下调用set设置元素,不能增加元素。


顺便研究了下java.util..ArrayList的add方法。

public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 在List的最后Append新元素,capacity增加一个
public void add(int index, E element) {
        rangeCheckForAdd(index);

        ensureCapacityInternal(size + 1);  // Increments modCount!!
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);
        elementData[index] = element;
        size++;
    }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 在index指定的位置增加元素E, 同时index右边的(索引>index) 所有元素右移;
  • 如果参数index大于当前的size会抛出异常,因为capacity只是增加一;
  • 因为存在“右移”操作,建议没有必要的情况下调用List#add(E e)即可,避免不必要的“右移”操作

demo 代码:

        String[] arrays = new String[] { "1", "2", "3" };
        List<String> list = Arrays.asList(arrays);
        System.out.println(list);
        System.out.println(list.getClass());
        List<String> list2 = new ArrayList<String>(list);
        list2.add(0, "0");
        System.out.println(list2);
        System.out.println(list.getClass());
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

结果输出:

[1, 2, 3]
class java.util.Arrays$ArrayList
[0, 1, 2, 3]
class java.util.Arrays$ArrayList
   
   
  • 1
  • 2
  • 3
  • 4
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值