您的位置:首页 > 编程语言 > Java开发

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

2017-11-24 09:36 239 查看
转自: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
[/code]

抛异常:

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
[/code]

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
[/code]

的确是实现了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
[/code]

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
[/code]

在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
[/code]

在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
[/code]

结果输出:

[1, 2, 3]
class java.util.Arrays$ArrayList
[0, 1, 2, 3]
class java.util.Arrays$ArrayList
1
2
3
4
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息