标签:int mod initial bool 线程安全 end The when 之一
List 接口概述:
鉴于 Java 中数组用来存储数据的局限性,我们通常使用 List 替代数组;
List 集合类中元素有序,可重复,集合中的每个元素都有对应的顺序索引;
List 容器中的元素都对应一个整数型的序号记载其在容器中的位置,可以根据序号存取容器中的元素;
JDK API中List接口的实现类常用的有:ArrayList、LinkedList 和 Vector;
面试题:ArrayList、LinkedList 和 Vector 三者的异同?
都实现 List 接口,也即存储数据有序,可重复;
ArrayList:作为 List 接口的主要实现类;线程不安全,效率高;底层使用 Object[ ] elementData 存储;
LinkedList:对于频繁的插入、删除操作,使用此类效率比 ArrayList 高;底层使用双向链表存储;
Vector:作为 List 接口的古老实现类;线程安全的,效率低;底层使用 Object[ ] elementData 存储;
ArrayList的底层源码分析:JDK 7 和 JDK 8 稍有不同
区别:jdk7 中的ArrayList的对象创建类似于单例的饿汉式,而jdk8中的ArrayList的对象创建类似于单例的懒汉式;
JDK8 下 ArrayList 源码分析
    /**
	 * Shared empty array instance used for default sized empty instances. We
	 * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
	 * first element is added.
	*/
	private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
	...
	public ArrayList() {
  	      this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
  	}
第一次调用 add 方法时,底层才创建长度为 10 的数组,并将数据123添加到 elementData[0],如果添加数据个数大于10 ,会进行扩容;
 /**
     * Default initial capacity.
     */
    private static final int DEFAULT_CAPACITY = 10;
	/**
     * Appends the specified element to the end of this list.
     *
     * @param e element to be appended to this list
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     */
    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
...
    private void ensureCapacityInternal(int minCapacity) {
        ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
    }
...
    private static int calculateCapacity(Object[] elementData, int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            return Math.max(DEFAULT_CAPACITY, minCapacity);
        }
        return minCapacity;
    }
...
    private void ensureExplicitCapacity(int minCapacity) {
        modCount++;
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
...
    /**
     * Increases the capacity to ensure that it can hold at least the
     * number of elements specified by the minimum capacity argument.
     *
     * @param minCapacity the desired minimum capacity
     */
    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);
    }
...
     private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }
LinkedList 源码分析
LinkedList list = new Linked(); 内部声明了Node类型的first和last属性,默认值为null;
list.add(123);将123封装带Node中,创建了Node对象;
其中 Node 定义为:
private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;
        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }
标签:int mod initial bool 线程安全 end The when 之一
原文地址:https://www.cnblogs.com/lililixuefei/p/13166740.html