先看私有属性
transient Object[] elementData;
private int size;
transient 关键字,就是这部分不参与序列化
构造函数
构造函数有三个
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
private static final Object[] EMPTY_ELEMENTDATA = {};
public ArrayList(
int initialCapacity) {
if (initialCapacity >
0) {
this.elementData =
new Object[initialCapacity];
}
else if (initialCapacity ==
0) {
this.elementData = EMPTY_ELEMENTDATA;
}
else {
throw new IllegalArgumentException(
"Illegal Capacity: "+
initialCapacity);
}
}
public ArrayList(Collection<?
extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) !=
0) {
if (elementData.getClass() != Object[].
class)
elementData = Arrays.copyOf(elementData, size, Object[].
class);
}
else {
this.elementData = EMPTY_ELEMENTDATA;
}
}
基本的方法
get方法
public E get(
int index) {
rangeCheck(
index);
return elementData(
index);
}
private void rangeCheck(
int index) {
if (
index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(
index));
}
@SuppressWarnings(
"unchecked")
E elementData(
int index) {
return (E) elementData[
index];
}
set方法
public E set(
int index, E element) {
rangeCheck(
index);
E oldValue = elementData(
index);
elementData[
index] = element;
return oldValue;
}
add方法
public boolean add(E e) {
ensureCapacityInternal(size +
1);
elementData[size++] = e;
return true;
}
public void add(
int index, E element) {
rangeCheckForAdd(
index);
ensureCapacityInternal(size +
1);
System.arraycopy(elementData,
index, elementData,
index +
1,
size -
index);
elementData[
index] = element;
size++;
}
private void rangeCheckForAdd(
int index) {
if (
index > size ||
index <
0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(
index));
}
addAll
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew);
System.arraycopy(a,
0, elementData, size, numNew);
size += numNew;
return numNew !=
0;
}
public boolean addAll(
int index, Collection<? extends E> c) {
rangeCheckForAdd(
index);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew);
int numMoved = size -
index;
if (numMoved >
0)
System.arraycopy(elementData,
index, elementData,
index + numNew,
numMoved);
System.arraycopy(a,
0, elementData,
index, numNew);
size += numNew;
return numNew !=
0;
}
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;
return oldValue;
}
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 void fastRemove(
int index) {
modCount++;
int numMoved = size -
index -
1;
if (numMoved >
0)
System.arraycopy(elementData,
index+
1, elementData,
index,
numMoved);
elementData[--size] =
null;
}
protected void removeRange(
int fromIndex,
int toIndex) {
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
int newSize = size - (toIndex-fromIndex);
for (
int i = newSize; i < size; i++) {
elementData[i] =
null;
}
size = newSize;
}
size方法
public int size() {
return size;
}
isEmpty()方法
public boolean isEmpty() {
return size ==
0;
}
indexOf方法(O(n))
public int indexOf(Object o) {
if (o ==
null) {
for (
int i =
0; i < size; i++)
if (elementData[i]==
null)
return i;
}
else {
for (
int i =
0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -
1;
}
public int lastIndexOf(Object o) {
if (o ==
null) {
for (
int i = size-
1; i >=
0; i--)
if (elementData[i]==
null)
return i;
}
else {
for (
int i = size-
1; i >=
0; i--)
if (o.equals(elementData[i]))
return i;
}
return -
1;
}
contains方法
public boolean contains(Object o) {
return indexOf(o) >=
0;
}
toArray方法
public Object[]
toArray() {
return Arrays.copyOf(elementData, size);
}
@SuppressWarnings(
"unchecked")
public <T> T[] toArray(T[]
a) {
if (
a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size,
a.getClass())
System.arraycopy(elementData,
0,
a,
0, size)
if (
a.length > size)
a[size] = null
return a
}
clear
public void clear() {
modCount++;
for (
int i =
0; i < size; i++)
elementData[i] =
null;
size =
0;
}
subList
public List<E>
subList(
int fromIndex,
int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(
this,
0, fromIndex, toIndex);
}
static void subListRangeCheck(
int fromIndex,
int toIndex,
int size) {
if (fromIndex <
0)
throw new IndexOutOfBoundsException(
"fromIndex = " + fromIndex);
if (toIndex > size)
throw new IndexOutOfBoundsException(
"toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException(
"fromIndex(" + fromIndex +
") > toIndex(" + toIndex +
")");
}
稍高级点儿的方法
add时候的空间检测
private static final int DEFAULT_CAPACITY =
10;
private void ensureCapacityInternal(
int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(
int minCapacity) {
modCount++;
if (minCapacity - elementData.length >
0)
grow(minCapacity);
}
private void grow(
int minCapacity) {
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >>
1);
if (newCapacity - minCapacity <
0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE >
0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(
int minCapacity) {
if (minCapacity <
0)
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
求elementData与一个集合的交集和差集
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c,
false);
}
public boolean retainAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c,
true);
}
private boolean batchRemove(Collection<?> c,
boolean complement) {
final Object[] elementData =
this.elementData;
int r =
0, w =
0;
boolean modified =
false;
try {
for (; r < size; r++)
if (c.contains(elementData[r]) == complement)
elementData[w++] = elementData[r];
}
finally {
if (r != size) {
System.arraycopy(elementData, r,
elementData, w,
size - r);
w += size - r;
}
if (w != size) {
for (
int i = w; i < size; i++)
elementData[i] =
null;
modCount += size - w;
size = w;
modified =
true;
}
}
return modified;
}
modCount
对于这个变量,大家可能会好奇是什么用,是为了多线程中判断是否被修改时使用,举个例子
public void sort(Comparator<?
super E> c) {
final int expectedModCount = modCount;
Arrays.sort((E[]) elementData,
0, size, c);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
至此,对ArrayList有了基本的了解,其中很重要的Itr没有说明,打算专门开一个说。