数据结构List之Vector

来源:互联网 发布:apache poi 读取excel 编辑:程序博客网 时间:2024/05/16 18:03

Vector和ArrayList都是以Array为底层的链表,它跟ArrayList的不同是它是线程安全的,因此它的速度慢,并且它的容量增长是2倍速度,下面的程序可以证明:

“`
package learnIng;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Vector;

public class VectorTest {
public static void main(String[] args) {
for (int j = 1; j < 100; j++) {
Vector squares = new Vector();
for (int i = 1; i < j; i++)
squares.add(i);
Class c = ((Object) squares).getClass();
Field f;
try {
f = c.getDeclaredField(“elementData”);
f.setAccessible(true);
try {
Object[] o = (Object[]) f.get(squares);
System.out.println(o.length);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
}
}
}“`
实验结果:
10
10
10
10
10
10
10
10
10
10
10
20
20
20
20
20
20
20
20
20
20
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
160
160
160
160
160
160
160
160
160
160
160
160
160
160
160
160
160
160
因此在大量数据插入的情况下我们选取vector.

0 0