arraylist下标从几开始_深入理解 ArrayList 及底层源码分析

cbfd4d697a560bd08917ad05ca7c2309.png

ArrayList 可能是 Java 数据结构中最简单的一种了,即使一个非 Java 程序员可能也知道这个数据结构,因为所有的语言中都有这样的类似的数据结构。但是在面试中很多人又对它又爱又恨,你真的了解 ArrayList 吗?

一、ArrayList 介绍及其源码剖析

1、什么是 ArrayList

可以简单的认为是一个动态数组;实际上 ArrayList 就是用数组实现的,长度不够时,调用 Arrays.copyOf 方法,拷贝当前数组到一个新的长度更大的数组;

ArrayList 是可调整大小的数组,实现了 List 接口。 实现所有可选列表操作,并允许所有元素包括 null 。除了实现 List 接口之外,该类还提供了一些方法来操纵内部使用的存储列表的数组的大小。 (这个类是大致相当于 Vector,不同之处在于它是不同步的)。

ArrayList 内部封装一个数组,用数组来存储数据。内部数组的默认初始容量 10,存满后 1.5 倍增长。从 JDK1.8 开始, ArrayList 一开始创建一个长度为 0 的数组,当添加第一个元素时再创建一个始容量为 10 的 数组。

2、ArrayList 的优缺点

1)优点

  • 能够自动扩容,默认每次扩容为原来容量大小的 1.5 倍。
  • 根据下标遍历元素,效率高。
  • 根据下标访问元素,效率高。

2)缺点

  • 线程不安全。
  • 在中间插入或删除元素的时候需要移动元素,效率低。

3、继承树结构

54afbf2d128db85becfa990890d3a58b.png
  • ArrayList 继承于 AbstractList,实现了 Cloneable、List、RandomAccess、Serializable 这些接口。
  • ArrayList 继承了 AbstractList,实现了 List。它是一个数组队列,提供了相关的添加、删除、修改、遍历等功能。
  • ArrayList 实现了 Cloneable 接口,即覆盖了函数 clone(),所以它能被克隆。
  • ArrayList 实现了 RandmoAccess 接口,因此提供了随机访问功能。
  • ArrayList 实现了 Serializable 接口,这意味着 ArrayList 支持序列化。

3、ArrayList 属性源码剖析

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable{
    // 序列化 id
    private static final long serialVersionUID = 8683452581122892189L;

    // 默认容量的大小为 10
    private static final int DEFAULT_CAPACITY = 10;

    // 空实例共享的一个空数组
    private static final Object[] EMPTY_ELEMENTDATA = {};

    // 默认的空数组常量为DEFAULTCAPACITY_EMPTY_ELEMENTDATA
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

    // transient不可序列化, 存放元素的数组
    transient Object[] elementData; // non-private to simplify nested class access

   // 元素的大小,默认是0
    private int size;

    ......
	
    // 最大数组容量为 Integer.MAX_VALUE – 8
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
}

二、构造方法及其源码剖析

1、带 int 类型的构造方法

/**
 * Constructs an empty list with the specified initial capacity.
 *
 * @param  initialCapacity  the initial capacity of the list
 * @throws IllegalArgumentException if the specified initial capacity
 *         is negative
 */
public ArrayList(int initialCapacity) {
    // 如果初始容量大于0
    if (initialCapacity > 0) {
    	// 新建一个初始容量大小的数组
        this.elementData = new Object[initialCapacity];
     // 如果初始容量为0
    } else if (initialCapacity == 0) {
    	// 将EMPTY_ELEMENTDATA赋值给 elementData
        this.elementData = EMPTY_ELEMENTDATA;
    } else {
    	// 否则初始容量小于0,则抛出异常 “非法的容量”
        throw new IllegalArgumentException("Illegal Capacity: "+
                                           initialCapacity);
    }
}
总结: ① 如果传入参数也就是初始容量大于0,则新建一个初始容量大小的数组; ② 如果传入的参数初始容量等于0,将 EMPTY_ELEMENTDATA 赋值给 elementData; ③ 如果传入的参数初始容量小于0,将抛出异常。

2、无参构造方法

/**
 * Constructs an empty list with an initial capacity of ten.
 */
public ArrayList() {
    // 没有指定初始容量,将成员变量elementData的值设为默认值
    this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
总结: 如果没有传入参数,则使用默认无参构建方法创建 ArrayList 对象。elementData 是一个大小为 0 的空数组。

3、Collection<? extends E> 型构造方法

/**
 * Constructs a list containing the elements of the specified
 * collection, in the order they are returned by the collection's
 * iterator.
 *
 * @param c the collection whose elements are to be placed into this list
 * @throws NullPointerException if the specified collection is null
 */
public ArrayList(Collection<? extends E> c) {
	// 将参数中的集合转换为数组,赋值给 elementData 
    elementData = c.toArray();
    // 如果数组的大小不等于 0
    if ((size = elementData.length) != 0) {
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        // 如果c.toArray()返回的数组类型不是Object[]类型的
        // 则利用Arrays.copyOf()创建一个大小为size的、类型为Object[]的数组, 并将elementData中的元素复制到新的数组中
        // 最后让elementData 指向新的数组
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, size, Object[].class);
    } else { // 否则设置元素数组为空数组
        // replace with empty array.
        this.elementData = EMPTY_ELEMENTDATA;
    }
}
总结: ① 将参数中的集合转换为数组,赋值给 elementData ② 给 size 进行赋值,size 代表集合元素数量。判断参数是否为空,如果数组的大小不等于 0,则进一步判断是否转化为 Object 类型的数组,如果不是,则进行复制; ③ 否则,设置元素数组为空数组

三、常用方法及其源码剖析

1、get(int index) 方法,返回集合中指定位置的元素

/**
 * Returns the element at the specified position in this list.
 *
 * @param  index index of the element to return
 * @return the element at the specified position in this list
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public E get(int index) {
    // 检查下标索引是否越界
    rangeCheck(index);

    // 返回指定索引处的值
    return elementData(index);
}
首先会通过 rangeCheck(int index) 方法检查索引值是否越界,如果索引值大于数组大小,则会抛出异常:
private void rangeCheck(int index) {
    // 索引值大于数组大小
    if (index >= size)
    	// 抛出异常
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
如果所引是合法的,则调用 elementData(int index) 方法获取指定位置的元素。

2. set(int index, E element) 方法,将index索引处的元素替换成element对象,返回被替换的旧元素

/**
 * Replaces the element at the specified position in this list with
 * the specified element.
 *
 * @param index index of the element to replace
 * @param element element to be stored at the specified position
 * @return the element previously at the specified position
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public E set(int index, E element) {
    // 检查下标索引是否越界
    rangeCheck(index);
	
    // 指定下标处的旧值
    E oldValue = elementData(index);
    // 指定下标处赋新的值
    elementData[index] = element;
    // 返回旧值
    return oldValue;
}

3、add(E e)方法,将指定的元素追加到此集合的末尾

/**
 * 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!!
    // 将元素添加到数组中最后一个元素的后面,最后将集合中实际的元素个数加 1
    elementData[size++] = e;
    return true;
}

4、add(int index, E element) 方法,在集合元素序列 index 位置处插入

/**
 * Inserts the specified element at the specified position in this
 * list. Shifts the element currently at that position (if any) and
 * any subsequent elements to the right (adds one to their indices).
 *
 * @param index index at which the specified element is to be inserted
 * @param element element to be inserted
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public void add(int index, E element) {
    // 检查下标索引是否越界
    rangeCheckForAdd(index);

    // 插入元素之前,会检查是否需要扩容
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    // 将 index 位置及其后面的所有元素都向后移一位
    System.arraycopy(elementData, index, elementData, index + 1,
                     size - index);
    // 将新元素插入到 index 位置处,元素个数加 1
    elementData[index] = element;
    size++;
}

5、remove(int index) 方法,删除该集合中指定位置的元素

/**
 * Removes the element at the specified position in this list.
 * Shifts any subsequent elements to the left (subtracts one from their
 * indices).
 *
 * @param index the index of the element to be removed
 * @return the element that was removed from the list
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
 public E remove(int index) {
        // 检查下标索引是否越界
        rangeCheck(index);

        modCount++;
        // 返回被删除的元素值
        E oldValue = elementData(index);
		
        // 需要移动的元素的个数
        int numMoved = size - index - 1;
        if (numMoved > 0)
            // 将 index + 1 及其后面的元素向前移动一位,覆盖被删除的元素值
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        // 把数组最后一个元素赋值为null,将元素个数减一
        elementData[--size] = null; // clear to let GC do its work
        // 返回旧值
        return oldValue;
    }

6、remove(Object o)方法是删除指定的元素,如果有重复的元素,则只会删除下标最小的元素

/**
  * Removes the first occurrence of the specified element from this list,
  * if it is present.  If the list does not contain the element, it is
  * unchanged.  More formally, removes the element with the lowest index
  * <tt>i</tt> such that
  * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
  * (if such an element exists).  Returns <tt>true</tt> if this list
  * contained the specified element (or equivalently, if this list
  * changed as a result of the call).
  *
  * @param o element to be removed from this list, if present
  * @return <tt>true</tt> if this list contained the specified element
  */
public boolean remove(Object o) {
    if (o == null) {
        for (int index = 0; index < size; index++)
            if (elementData[index] == null) {
                fastRemove(index);
                return true;
            }
    } else {
    	// 遍历数组,查找要进行删除元素的位置
        for (int index = 0; index < size; index++)
            if (o.equals(elementData[index])) {
                fastRemove(index);
                return true;
            }
    }
    return false;
}

7、fastRemove(int index)方法,删除跳过边界检查的方法,也不返回删除的元素值

/*
 * Private remove method that skips bounds checking and does not
 * return the value removed.
 */
private void fastRemove(int index) {
	   modCount++;
	   int numMoved = size - index - 1;
	   if (numMoved > 0)
	       System.arraycopy(elementData, index+1, elementData, index,
	                        numMoved);
	   elementData[--size] = null; // clear to let GC do its work
}

8、size() 方法,返回集合中的元素个数

/**
 * Returns the number of elements in this list.
 *
 * @return the number of elements in this list
 */
public int size() {
    return size;
}

9、indexOf(Object o) 方法,返回对象o在集合中第一次出现的位置的索引

/**
 * Returns the index of the first occurrence of the specified element
 * in this list, or -1 if this list does not contain the element.
 * More formally, returns the lowest index <tt>i</tt> such that
 * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
 * or -1 if there is no such index.
 */
public int indexOf(Object o) {
        // 如果查找的元素为空
        if (o == null) {
            // 循环遍历数组
            for (int i = 0; i < size; i++)
            	// 如果 i 位置的原素为空 
                if (elementData[i]==null)
                    // 返回下标
                    return i;
        // 否则,查找的元素不为空
        } else {
            // 循环遍历数组
            for (int i = 0; i < size; i++)
            	// 找到第一个和要查找的元素相等的元素
                if (o.equals(elementData[i]))
                   // 返回下标
                    return i;
        }
        // 返回 -1.则表示没有找到
        return -1;
}

四、对扩容原理源码剖析及其思考

1、在 add() 方法中调用 ensureCapacityInternal() 方法,用来确保添加元素的时候,最小容量是 minCapacity

private void ensureCapacityInternal(int minCapacity) {
	// 判断元素数组是否为空数组
    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
    	// 取最大值
        minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
    }

    ensureExplicitCapacity(minCapacity);
}

2、在 ensureCapacityInternal() 方法里面调用 ensureExplicitCapacity(int minCapacity) 方法,用来判段集合在添加元素的时候是否需要对当前的数组进行扩容

private void ensureExplicitCapacity(int minCapacity) {
   modCount++;

    // overflow-conscious code
    // 判断minCapacity与当前元素数组的长度的大小
    // 如果minCapacity比当前元素数组的长度的大小大的时候需要扩容
    if (minCapacity - elementData.length > 0)
    	// 扩容
        grow(minCapacity);
}

3、在 ensureExplicitCapacity() 方法里面调用 grow(int 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;
    // 新的容量是原本容量的1.5倍
    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);
}

4、hugeCapacity(int minCapacity) 方法,用于处理 minCapacity 超出 MAX_ARRAY_SIZE 的情况

private static int hugeCapacity(int minCapacity) {
    if (minCapacity < 0) // overflow
        throw new OutOfMemoryError();
    return (minCapacity > MAX_ARRAY_SIZE) ?
        Integer.MAX_VALUE :
        MAX_ARRAY_SIZE;
}

5、最后调用 Arrays.copyOf() 方法,用来复制原数组,将 elementData 赋值为得到的新数组。

注意: 由于数组复制的代价比较大,因此建议在创建 ArrayList 对象的时候就指定大概的容量大小,从而减少扩容操作的次数

五、ArrayList 和 Vector 的区别

  • ArrayList 和 Vector 底层都是数组
  • ArrayList 每次扩容的情况下扩容为原来的 1.5 倍。线程不安全,当多个线程同时访问同一个 ArrayList 集合时,如果两个或两个以上的线程修改了 ArrayList 集合,则必须手动保证该集合的同步性。
  • Vector 是同步类,其线程安全,但是它的访问比较慢。Vector 每次扩容为其空间大小的 2 倍。
知乎视频​www.zhihu.com

希望能够帮到大家,有问题的可以留言讨论哦!

67d06e24656f281e75010376a4bad7eb.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值