ArrayList源码分析底层原理
public class ArrayList<E> implements List<E> {
private int size;
transient Object[] elementData;
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
private static final Object[] EMPTY_ELEMENTDATA = {};
private static final int DEFAULT_CAPACITY = 10;
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
protected transient int modCount = 0;
public ArrayList() {
elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("参数有误,initialCapacity:" + initialCapacity);
}
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public boolean contains(Object o) {
if (o == null) {
for (int i = 0; i < size; i++) {
if (elementData(i) == null) {
return true;
}
}
return false;
}
for (int i = 0; i < size; i++) {
if (o.equals(elementData(i))) {
return true;
}
}
return false;
}
@Override
public boolean add(E e) {
ensureCapacityInternal(size + 1);
elementData[size++] = e;
return true;
}
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;
}
@Override
public boolean remove(Object o) {
if (o == null) {
for (int i = 0; i < size; i++) {
if (elementData(i) == null) {
fastRemove(i);
return true;
}
}
return false;
}
for (int i = 0; i < size; i++) {
if (o.equals(elementData(i))) {
fastRemove(i);
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;
}
@Override
public void clear() {
modCount++;
for (int i = 0; i < size; i++) {
elementData[i] = null;
}
size = 0;
}
@Override
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
private void rangeCheck(int index) {
if (index >= size)
throw new ArrayIndexOutOfBoundsException("下标越界,index:" + index);
}
private E elementData(int index) {
return (E) elementData[index];
}
@Override
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
@Override
public void add(int index, E element) {
rangeCheck(index);
ensureCapacityInternal(size + 1);
int moveNum = size - index;
System.arraycopy(elementData, index, elementData, index + 1, moveNum);
elementData[index] = element;
size++;
}
@Override
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 static void main(String[] args) {
List<Integer> nums = new ArrayList<Integer>();
for (int i = 0; i < 50; i++) {
nums.add(i + 1);
}
for (int i = 0; i < nums.size(); i++) {
System.out.print(nums.get(i) + "\t");
}
System.out.println("\n删除下标9");
nums.remove(9);
for (int i = 0; i < nums.size(); i++) {
System.out.print(nums.get(i) + "\t");
}
System.out.println("\n在下标9的位置新增元素10");
nums.add(9, 10);
for (int i = 0; i < nums.size(); i++) {
System.out.print(nums.get(i) + "\t");
}
System.out.println("\n修改下标9,将值改为666");
nums.set(49, 666);
for (int i = 0; i < nums.size(); i++) {
System.out.print(nums.get(i) + "\t");
}
System.out.println("\n是否包含元素666--->isFlag:" + nums.contains(666));
System.out.println("size:" + nums.size());
}
}