定义:
public class ArrayList extends AbstractList
implements List, RandomAccess, Cloneable, java.io.Serializable
ArrayList类继承自AbstractList,实现了List接口。这里有个小问题:AbstractList已经实现了List接口,为什么ArrayList还要去实现一次呢?其实,不止ArrayList是这样的,Collection框架中有很多类都是这样的。原因很简单,只是为了清晰的标明是对应接口的实现 。ArrayList同时实现了Cloneable接口和Serializable接口,使得ArrayList具有克隆和序列化的功能
成员变量:
private static final long serialVersionUID = 8683452581122892189L;
//默认初始容量
private static final int DEFAULT_CAPACITY = 10;
//空对象数组,用于构造空对象数组
private static final Object[] EMPTY_ELEMENTDATA = {};
//空对象数组,用于默认构造方法初始化对象数组
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
//存放数据的对象数组 可以看出是数组实现
transient Object[] elementData;
//List中元素的个数(跟容量不是一回事)
private int size;
构造方法
首先从构造方法入手,看看在创建一个ArrayList对象的时候都做了什么,这没什么难度。
//默认构造方法,创建一个空list对象
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
//带初始容量参数的构造方法,创建一个指定容量的list对象
//指定容量作为参数的构造函数,当参数大于0,elementData初始化为指定容量大小的数组;当参数等于0,elementData初始化为空常量数组EMPTY_ELEMENTDATA;当参数小于0,抛出异常。
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);
}
}
//带指定元素的构造方法,创建一个包含指定元素的list对象
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;
}
}
主要方法
1、 public boolean add(E e)
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
add()首先调用ensureCapacityInternal(int minCapacity)
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
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);
}
2、 public void add(int index, E element)
功能:在list的指定位置添加元素
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1); // Increments modCount!!
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));
}
首先做位置有效性检查,如果超出范围则抛出异常。调用native拷贝方法,将指定位置元素及其后的元素往后挪动一个位置
3、 public boolean addAll(int index, Collection<? extends E> c)
功能:在list的指定位置添加集合
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // Increments modCount
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;
}
4、 public boolean addAll(Collection<? extends E> c)
功能:在list的末尾添加集合
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // Increments modCount
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
5、public E get(int index)
功能:获取指定位置的元素
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
//判断一下索引有没有下标越界
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
6、public E set(int index, E element)
功能:更改指定位置的元素
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
7、public E remove(int index)
功能:删除指定位置的元素
public E remove(int index) {
//检查下标是否越界
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
//elementData源数组,index+1起始位置,elementData目标数组,index目标数组起始位置,numMoved需要复制的元素个数
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
//将末尾元素置空,等待被垃圾回收
elementData[--size] = null;
return oldValue;
}
8、public boolean remove(Object o)
功能:删除第一个指定的元素
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; // clear to let GC do its work
}
9、public void clear()
功能:清空列表
public void clear() { modCount++; // clear to let GC do its work for (int i = 0; i < size; i++) elementData[i] = null; size = 0; }
转载https://blog.csdn.net/LeonSong2000/article/details/52791907