ArrayList
- 主要成员变量:
transient Object[] elementData; //底层是一个数组结构,用来保存元素
private static final int DEFAULT_CAPACITY = 10;//默认初始化容量大小为10
构造方法
主要构造方法: public ArrayList(int initialCapacity)
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
//创建一个指定大小的Object数组
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
add方法
add(E e)&add(int index, E element)两个重载
public boolean add(E e) {
//1. 确定list的空间大小
ensureCapacityInternal(size + 1); // Increments modCount!!
//2. 在数组从左向右第一个为null的位置填充e
elementData[size++] = e;
return true;
}
ensureCapacityInternal(int minCapacity)方法:相当于grow方法的一个前奏吧
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
//最小的容量为10
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
//如果最大容量已经达到,对数组进行扩容
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
grow方法:就是用来对底层的数组结构进行扩容
private void grow(int minCapacity) {
// 旧的数组长度
int oldCapacity = elementData.length;
//新的数组长度 = 旧的数组长度 + 旧的数组长度*0.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:
//把原来的数组里面的元素复制到新数组里面,并且把当前list的元素指针指向新的数组
elementData = Arrays.copyOf(elementData, newCapacity);
}
get方法
这个方法就是返回执行下标位置的元素
public E get(int index) {
//检查数组下标是否越界
rangeCheck(index);
return elementData(index);
}
remove方法
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
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
return oldValue;
}
/**
* 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 ? get(i)==null : 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;
}
/*
* 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
}
public static native void arraycopy(Object src, int srcPos,Object dest, int destPos,
int length);😒
这个方法主要就是完成数组的复制