ArrayList: 内部是数组实现的列表
/**
* The number of elements in this list. 实际有效的数据数量
*/
int size;
/**
* The elements in this list, followed by nulls. 数组实现
transient: 表示不可序列号
*/
transient Object[] array;
增:考虑是否扩容
@Override
public void add(int index, E object) {
Object[] a = array;
int s = size;
if (index > s || index < 0) {
throw IndexOutOfBoundsException(index, s);
}
if (s < a.length) {
System.arraycopy(a, index, a, index + 1, s - index);
} else {
// assert s == a.length;
Object[] newArray = new Object[newCapacity(s)];
System.arraycopy(a, 0, newArray, 0, index);
System.arraycopy(a, index, newArray, index + 1, s - index);
array = a = newArray;
}
a[index] = object;
size = s + 1;
modCount++;
}
删:
@Override
public E remove(int index) {
Object[] a = array;
int s = size;
if (index >= s) {
throwIndexOutOfBoundsException(index, s);
}
@SuppressWarnings("unchecked")
E result = (E) a[index];
System.arraycopy(a, index + 1, a, index, --s - index);
a[s] = null; // Prevent memory leak
size = s;
modCount++;
return result;
}
改:
@Override
public E set(int index, E object) {
Object[] a = array;
if (index >= size) {
throwIndexOutOfBoundsException(index, size);
}
@SuppressWarnings("unchecked")
E result = (E) a[index];
a[index] = object;
return result;
}
查:
对null的特殊处理
@Override public boolean contains(Object object) {
Object[] a = array;
int s = size;
if (object != null) {
for (int i = 0; i < s; i++) {
if (object.equals(a[i])) {
return true;
}
}
} else {
for (int i = 0; i < s; i++) {
if (a[i] == null) {
return true;
}
}
}
return false;
}
清理 clear:
@Override public void clear() {
if (size != 0) {
Arrays.fill(array, 0, size, null);
size = 0;
modCount++;
} }
扩容:
private static int newCapacity(int currentCapacity) {
int increment = (currentCapacity < (MIN_CAPACITY_INCREMENT / 2) ? MIN_CAPACITY_INCREMENT : currentCapacity >> 1);
return currentCapacity + increment;
}
用到的工具方法
/**
* Copies {@code length} elements from the array {@code src},
* starting at offset {@code srcPos}, into the array {@code dst},
* starting at offset {@code dstPos}.
*
* <p>The source and destination arrays can be the same array,
* in which case copying is performed as if the source elements
* are first copied into a temporary array and then into the
* destination array.
*
* @param src
* the source array to copy the content.
* @param srcPos
* the starting index of the content in {@code src}.
* @param dst
* the destination array to copy the data into.
* @param dstPos
* the starting index for the copied content in {@code dst}.
* @param length
* the number of elements to be copied.
*/
public static native void arraycopy(Object src, int srcPos,
Object dst, int dstPos, int length);
/**
* Fills the specified range in the array with the specified element.
*
* @param array
* the {@code Object} array to fill.
* @param start
* the first index to fill.
* @param end
* the last + 1 index to fill.
* @param value
* the {@code Object} element.
* @throws IllegalArgumentException
* if {@code start > end}.
* @throws ArrayIndexOutOfBoundsException
* if {@code start < 0} or {@code end > array.length}.
*/
public static void fill(Object[] array, int start, int end, Object value) {
Arrays.checkStartAndEnd(array.length, start, end);
for (int i = start; i < end; i++) {
array[i] = value;
}
}
/**
* Checks that the range described by {@code start} and {@code end} doesn't exceed
* {@code len}.
*
* @hide
*/
public static void checkStartAndEnd(int len, int start, int end) {
if (start < 0 || end > len) {
throw new ArrayIndexOutOfBoundsException("start < 0 || end > len."
+ " start=" + start + ", end=" + end + ", len=" + len);
}
if (start > end) {
throw new IllegalArgumentException("start > end: " + start + " > " + end);
}
}