ArrayList源码:

    private static final int DEFAULT_CAPACITY = 10;//默认长度

    /**
* Shared empty array instance used for empty instances.
*/
private static final Object[] EMPTY_ELEMENTDATA = {}; //默认数组 /**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == EMPTY_ELEMENTDATA will be expanded to
* DEFAULT_CAPACITY when the first element is added.
*/
private transient Object[] elementData; //存放数据的数组 /**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size; /**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
} public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
} private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
//判断添加的第几个元素是否比数组长度大,是的话扩容
ensureExplicitCapacity(minCapacity);
} private void ensureExplicitCapacity(int minCapacity) {
modCount++; // overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
} private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);//扩容两倍
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}

LinkedList源码:

   public boolean add(E e) {
linkLast(e);
return true;
} void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;//需要添加的节点记作上一个节点,供下次使用。
if (l == null)
first = newNode;//第一个添加进来的作为第一个节点
else
l.next = newNode;//记住下一个节点
size++;
modCount++;
}

LinkedList还有linkBefore、linkFirst...等等其他方法。

Vector源码:

    public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
} public Vector(int initialCapacity) {
this(initialCapacity, 0);
} public Vector() {
this(10);
}

Vector的三个构造函数

    //同步方法
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
} private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
//判断是否需要扩容
if (minCapacity - elementData.length > 0)
grow(minCapacity);
} private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);//扩原来的一倍或者是设置扩容步长,每次增加步长长度。
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}

list和set的toString方法是在AbstractCollection类重写了。

总结:LinkedList插入快,查询慢,ArrayList查询快,插入慢,扩容长度为原来的一半,可以给定数组长度。Vector可以给定长度和扩容步长。没有给定步长,扩容一倍,给定了,每次增加步长。

ArrayList和LinkedList和Vector源码分析的更多相关文章

  1. ArrayList、LinkedList和Vector源码分析

    ArrayList.LinkedList和Vector源码分析 ArrayList ArrayList是一个底层使用数组来存储对象,但不是线程安全的集合类 ArrayList的类结构关系 public ...

  2. Java集合之Vector源码分析

    概述 Vector与ArrayLIst类似, 内部同样维护一个数组, Vector是线程安全的. 方法与ArrayList大体一致, 只是加上 synchronized 关键字, 保证线程安全, 下面 ...

  3. Stack和Vector源码分析

    Stack和Vector源码分析 Stack和Vector源码分析stack源码分析1.Stack是什么2.Stack的结构图3.Stack继承关系4.Stack的主要方法5.Stack源码Vecto ...

  4. Vector源码分析和实例应用

    1.Vector介绍 Vector 是矢量队列,它是JDK1.0版本添加的类.继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口. Vector ...

  5. LinkedList详解-源码分析

    LinkedList详解-源码分析 LinkedList是List接口的第二个具体的实现类,第一个是ArrayList,前面一篇文章已经总结过了,下面我们来结合源码,学习LinkedList. 基于双 ...

  6. Vector源码分析

    Vector与ArrayList底层实现基本类似,底层都是用数组实现的,最大的不同是Vector是线程安全的.ArrayList源码分析请参考ArrayList源码分析 一.源码分析 基于jdk1.7 ...

  7. 集合之ArrayList(含JDK1.8源码分析)

    一.ArrayList的数据结构 ArrayList底层的数据结构就是数组,数组元素类型为Object类型,即可以存放所有类型数据.我们对ArrayList类的实例的所有的操作(增删改查等),其底层都 ...

  8. [Java]Vector源码分析

    第1部分 Vector介绍 Vector简介 Vector也是基于数组实现的,是一个动态数组,其容量能自动增长.继承于AbstractList,实现了List, RandomAccess, Clone ...

  9. ArrayList用法详解与源码分析

    说明 此文章分两部分,1.ArrayList用法.2.源码分析.先用法后分析是为了以后忘了查阅起来方便-- ArrayList 基本用法 1.创建ArrayList对象 //创建默认容量的数组列表(默 ...

随机推荐

  1. openSuse快捷键

    1.printscreen全屏截图 2.ctrl+printscreen窗口截图 3.shift+printscreen选择截图 4.Ctrl+Alt+up arrow.Ctrl+Alt+down a ...

  2. 关于 Unchecked cast from Iterator to Iterator String 提示

    遇到个这个提示:Unchecked cast from Iterator to Iterator String Iterator<String> keys = data.keys(); 修 ...

  3. C#中的委托到底是什么概念??

    委托,简单理解是这样的.比如您要管您的孩子,把孩子送进了幼儿园.OK.此时您就把您的孩子委托给了幼儿园.当幼儿园放学,将孩子交还给您的手中.则是委托的回调.当然我这里的例子是说异步委托调用.您也可以同 ...

  4. umount: /home: device is busy

    转自:umount: /home: device is busy 取消挂载/home时出现umount: /home: device is busy,原因是因为有程序在使用/home目录,我们可以使用 ...

  5. Dylans loves numbers

    Problem Description Who is Dylans?You can find his ID in UOJ and Codeforces. His another ID is s1451 ...

  6. ios 显示其他app的购买页面

    using UnityEngine; using System.Collections; using System.Runtime.InteropServices ; public class IOS ...

  7. extjs中,datefield日期,点击输入框弹出日期,禁止手动输入

    之前用readonly:true连日期也选不了了,editable:false也不行,最后用事件监听实现 栗子如下: { xtype: 'datefield', fieldLabel: '创建结束时间 ...

  8. Web Service循序渐进学习

    1.Web service 是什么? (WebService 是可以通过互联网远程访问调用的应用程序,实现数据传输共享.这种应用程序理解为不同服务提供帮助.能够支撑实现应用程序发布访问的技术可以称为W ...

  9. jquery常用的选择器

    jquery用选择器来得到jquery对象,进而进行一些操作. 一.基本选择器 1.id选择器.示例:选择id为one的元素 var $one = $("#one"); 2.类选择 ...

  10. IE8及以下不支持getElementByClassName的解决办法

    function getByClass(oParent, sClass){ var aEle=oParent.getElementsByTagName('*'); var aResult=[]; va ...