先看看ArrayList源码
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
//其中有一个成员变量
private transient E[] elementData;
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = (E[])new Object[initialCapacity];
}
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this(10);
}
由此我们可以看出它的底层其实是用数组来维护的,而且默认数组大小为10,当我们向ArrayList添加一个对象时,此对象交由数组来指向,当添加的对象超过10时
public boolean add(E o) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = o;
return true;
}
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
elementData = (E[])new Object[newCapacity];
System.arraycopy(oldData, 0, elementData, 0, size);
}
}
我们可以看到会生成一个新的对象数组,长度为原长度的1.5倍加上1,当再次超过重复此过程
再来看看LinkedList
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Queue<E>, Cloneable, java.io.Serializable
{
private transient Entry<E> header = new Entry<E>(null, null, null);
private transient int size = 0;
/**
* Appends the specified element to the end of this list.
*
* @param o element to be appended to this list.
* @return <tt>true</tt> (as per the general contract of
* <tt>Collection.add</tt>).
*/
public boolean add(E o) {
addBefore(o, header);
return true;
}
private Entry<E> addBefore(E o, Entry<E> e) {
Entry<E> newEntry = new Entry<E>(o, e, e.previous);
newEntry.previous.next = newEntry;
newEntry.next.previous = newEntry;
size++;
modCount++;
return newEntry;
}
private static class Entry<E> {
E element;
Entry<E> next;
Entry<E> previous;
Entry(E element, Entry<E> next, Entry<E> previous) {
this.element = element;
this.next = next;
this.previous = previous;
}
}
很明显LinkedList的底层是用双向链表来实现的,当向LinkedList添加一个对象时候,实际上在底层生成了一个Entry对象,并将添加的对象赋给其成员变量element,然后Entry会构造前后引用,使得前后链接,简单说LinkedList维护的并不是对象本身,其实是Entry对象
从上面可以看出
ArrayList是用数组来实现,所以查询的效率要高,而LinkedList是用链表来实现,所以其插入和删除的效率要高,但是两者都不是线程安全的,所以在实际开发中都要进行同步
分享到:
相关推荐
1.List是接口类,ArrayList和LinkedList是List的实现类 2.ArrayList是动态数组(顺序表)的数据结构 3.LinkedList
测试ArrayList和LinkedList的add方法
关于arraylist和linkedList的区别
ArrayList、LinkedList、Vector区别简介。
【Java面试题】ArrayList和LinkedList区别
合理运用ArrayList与LinkedList
一般大家都知道ArrayList和LinkedList的大致区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。 2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要...
05丨ArrayList还是LinkedList?使用不当性能差千倍.html
ArrayList Vector LinkedList 区别与用法.
1,ArrayList是数组的数据结构,LinkedList是链表的数据结构。 2,随机访问的时候,ArrayList的效率比较高,因为LinkedList要移动指针,而ArrayList是基于 3,索引(index)的数据结构,可以直接映射到。 4,插入、...
2.在ArrayList的中间插入或删除一个元素意味着这个列表中剩余的元素都会被移动 3.LinkedList不支持高效的随机元素访问 4.ArrayList的
比较ArrayList,LinkedList,Vector三者随机读取,插入,删除性能。
list集合案例增、删、改、查,ArrayList与LinkedList的区别,LinkedList堆栈/队列的开发,list集合容量会自动扩容,list去除重复
对比Vector、ArrayList、LinkedList1
ArrayList-LinkedList-源码.rar
10.ArrayList 和LinkedList的区别.avi
Java ArrayList Vector LinkedList map区别 各种集合的区别 写得非常详细
集合(Arraylist,LinkedList)进阶思维导图
Map+List+ArrayList+LinkedList Java源代码,适合初学者
ArrayList与LinkedListArrayList与LinkedList扩容数组最大容量默认的初始容量扩容新容量=旧容量*1.5* Increases