博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ArrayList和LinkedList和Vector源码分析
阅读量:4687 次
发布时间:2019-06-09

本文共 4062 字,大约阅读时间需要 13 分钟。

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
l = last; final Node
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可以给定长度和扩容步长。没有给定步长,扩容一倍,给定了,每次增加步长。

转载于:https://www.cnblogs.com/hjy9420/p/4254057.html

你可能感兴趣的文章
RSA System.Security.Cryptography.CryptographicException
查看>>
s的封装和信息隐蔽
查看>>
excelhttp://www.cnblogs.com/caoyuanzhanlang/p/3591904.html
查看>>
ArrayList和LinkedList和Vector源码分析
查看>>
webservice整合spring cxf
查看>>
再次编译这个应用程序应该不会有问题
查看>>
Ubuntu-tomcat7目录
查看>>
189. Rotate Array
查看>>
asp.net 的三种开发模式
查看>>
Android 交叉编译 IPerf3
查看>>
Android原生Gallery关于图像Orientation的问题
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>
Entity Framework 学习高级篇1—改善EF代码的方法(上)
查看>>
Mybatis逆向工程配置文件详细介绍(转)
查看>>
数组 Arrays类
查看>>
String类的深入学习与理解
查看>>
OLTP vs OLAP
查看>>
一些题目(3)
查看>>
Eclipse下配置主题颜色
查看>>
杂题 UVAoj 107 The Cat in the Hat
查看>>