1.属性介绍
/**
* 初始化默认容量。
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* 指定该ArrayList容量为0时,返回该空数组。
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
/**
* 当调用无参构造方法,返回的是该数组。刚创建一个ArrayList 时,其内数据量为0。
* 它与EMPTY_ELEMENTDATA的区别就是:该数组是默认返回的,而后者是在用户指定容量为0时返回。
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
* 保存添加到ArrayList中的元素。
* ArrayList的容量就是该数组的长度。
* 该值为DEFAULTCAPACITY_EMPTY_ELEMENTDATA 时,当第一次添加元素进入ArrayList中时,数组将扩容值DEFAULT_CAPACITY。
* 被标记为transient,在对象被序列化的时候不会被序列化。
*/
transient Object[] elementData; // non-private to simplify nested class access
/**
* ArrayList的实际大小(数组包含的元素个数)。
* @serial
*/
private int size;2. 构造方法
ArrayList提供了三种构造方法。
ArrayList(int initialCapacity):构造一个指定容量为capacity的空ArrayList。
ArrayList():构造一个初始容量为 10 的空列表。
ArrayList(Collection<? extends E> c):构造一个包含指定 collection 的元素的列表,这些元素是按照该 collection 的迭代器返回它们的顺序排列的。
/**
* 构造一个指定初始化容量为capacity的空ArrayList。
*
* @param initialCapacity ArrayList的指定初始化容量
* @throws IllegalArgumentException 如果ArrayList的指定初始化容量为负。
*/
public ArrayList(int initialCapacity) {
//判断指定的初始化容量,如果大于0,则新建一个Object数组,否则返回上述属性中的EMPTY_ELEMENTDATA
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
ArrayList()
/**
* 构造一个初始容量为 10 的空列表。
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
ArrayList(Collection<? extends E> c)
/**
* 构造一个包含指定 collection 的元素的列表,这些元素是按照该 collection 的迭代器返回它们的顺序排列的。
*
* @param c 其元素将放置在此列表中的 collection
* @throws NullPointerException 如果指定的 collection 为 null
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}3.主要方法:
构造方法介绍完之后,一起看一下主要的操作方法:
get方法:
介绍:get方法相对简单,判断是否越界,之后通过数组下标来获取数据。所以get方法的时间复杂度为O(1)。
/**
* 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) {
//检查下标index是否越界,如果下标大于当前的size,那么会报下标越界异常
rangeCheck(index);
//返回具体的下标的值
return elementData(index);
}
/**
* Checks if the given index is in range. If not, throws an appropriate
* runtime exception. This method does *not* check if the index is
* negative: It is always used immediately prior to an array access,
* which throws an ArrayIndexOutOfBoundsException if index is negative.
*/
private void rangeCheck(int index) {
//检测下标是否越界
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
/**
* 返回索引为index的元素
*/
@SuppressWarnings("unchecked")
E elementData(int index) {
return (E) elementData[index];
}add方法:
/**
* 添加元素到list末尾.
*
* @param e 被添加的元素
* @return true
*/
public boolean add(E e) {
//确认list容量,如果不够,容量加1。
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;
//这个是扩容的重点,oldCapacity >> 1右移一位,然后再+oldCapacity,相当于扩容为原先的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);
}
private static int hugeCapacity(int minCapacity) {
//判断入参容量的大小
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
//和最大容量进行比较,如果大于最大容量,则扩容后最大容量为定义的最大容量,不会无限制扩容
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}set方法:
/**
* 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;
}remove方法:
/**
* 删除list中位置为指定索引index的元素
* 索引之后的元素向左移一位
*
* @param index 被删除元素的索引
* @return 被删除的元素
* @throws IndexOutOfBoundsException 如果参数指定索引index>=size,抛出一个越界异常
*/
public E remove(int index) {
//检查索引是否越界。如果参数指定索引index>=size,抛出一个越界异常
rangeCheck(index);
//结构性修改次数+1
modCount++;
//记录索引为inde处的元素
E oldValue = elementData(index);
// 删除指定元素后,需要左移的元素个数
int numMoved = size - index - 1;
//如果有需要左移的元素,就移动(移动后,该删除的元素就已经被覆盖了)
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
// size减一,然后将索引为size-1处的元素置为null。为了让GC起作用,必须显式的为最后一个位置赋null值
elementData[--size] = null; // clear to let GC do its work
//返回被删除的元素
return oldValue;
}
/**
* 越界检查。
* 检查给出的索引index是否越界。
* 如果越界,抛出运行时异常。
* 这个方法并不检查index是否合法。比如是否为负数。
* 如果给出的索引index>=size,抛出一个越界异常
*/
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

0条评论
点击登录参与评论