1 / 12
文档名称:

ArrayList、LinkedList、 Vector、Map 用法比较.pdf

格式:pdf   页数:12
下载后只包含 1 个 PDF 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

ArrayList、LinkedList、 Vector、Map 用法比较.pdf

上传人:翩仙妙玉 2012/9/15 文件大小:0 KB

下载得到文件列表

ArrayList、LinkedList、 Vector、Map 用法比较.pdf

文档介绍

文档介绍:IT-Homer 专栏
成功是优点的发挥,失败是缺点的积累! 不为失败找理由,只为成功找
方法……
ArrayList、LinkedList、 Vector、Map 用法比较
分类: Java/JSP 2012-06-15 18:17 685人阅读评论(0) 收藏举报
ArrayList和Vector是采用数组方式存储数据,此数组元素总数大于实际存储的数据个数以便增加和插入
元素,二者都允许直接序号索引元素,但是插入数据要移动数组元素等内存操作,所以它们索引数据快、
插入数据慢。
ArrayList数组存储方式:
private transient Object[] elementData;
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
= new Object[initialCapacity];
}
// 空构造函数,默认容量大小为10
public ArrayList() {
this(10);
}
public ArrayList(Collection<? extends E> c) {
elementData = ();
size = ;
// might (incorrectly) not return Object[] (see 6260652)
if (() != Object[].class)
elementData = (elementData, size, Object[].class);
}
Vector由于使用了synchronized同步方法(如add、insert、remove、set、equals、hashcode等操
作),因此是线程安全,性能上比ArrayList要差。
Vector数组存储方式:
protected Object[] elementData;
protected int capacityIncrement;
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
= new Object[initialCapacity];
= capacityIncrement;
}
// 设置容量大小为initialCapacity,默认增长个数为0
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
1
// 空构造函数,默认容量大小为10
public Vector() {
this(10);
}
LinkedList使用双向链表实现存储,按序号索引数据需要进行向前或向后遍历,但是插入数据时只需要记
录本项的前后项即可,所以插入数度较快!
LinkedList双向链表,是指可以从first依次遍历至last(从头到尾),也可以从last遍历至first(从尾到头),但
首尾没有构成环,不同于双向循环链表(注意区分):
transient Node<E> first;
transient Node<E> last;
public LinkedList() {
}
private void linkFirst(E e) {
final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f); // 插入新节点,同时连接首、尾节点
first = newNode;
if (f == null) // 起始节点为空(null),表示插入后有且只有一个节点,因此first = last = newNode
last = newNode;
else
= newNode;