源码解析
ArrayList ---- 数据结构
private static final int DEFAULT_CAPACITY = 10;
private static final Object[] EMPTY_ELEMENTDATA= {};
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
transient Object[] elementData;
int size = 0;
// 构造方法
// 1
public ArrayList(){
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
// 2
public ArrayList(int initCapacity){
if(initCapacity > 0){
this.elementData = new Object[initCapacity];
}else if(initCapcity == 0){
this.elementData = EMPTY_ELEMENTDATA;
}else{
throw new IllegalArgumentException...
}
}
// 3
public ArrayList(Collection <? extends E> c){
elementData = c.toArray();
if((size = elementData.length) != 0) {
if(elementData.getClass() != Object[].class){
// if elementData is Object type so that we dont need to oprate it.
elementData = Arrays.copyOf(elementData,size,Object[].class);
}
} else {
this.elementData = EMPTY_ELEMENTDATA;
}
}
ArrayList ---- ADD
// ADD
public boolean add(E e){
ensureCapacityInternal(size+1);
elementData[size++] = e;
return true;
}
private ensureCapacityInternal(int minCapacity){
if(elementData == DEFAULT_EMPTY_ELEMENTDATA ){
minCapacity = Math.max(DEFAULT_CAPACITY,minCapacity);
}
ensureCapacityExplicit(minCapacity);
}
private ensureCapacityExpicit(int minCapacity){
modCount++;
if(minCapacity - elementData.length > 0){
grow(minCapacity);
}
}
private grow(int minCapacity){
modCount++;
int oldValue = elementData.length;
int newValue = oldValue + oldValue >> 1 ;
if(newValue - minCapacity < 0){
newValue = minCapacity;
}
if(newValue - MAX_ARRAY_SIZE > 0 ){
newValue = hugeCapacity(minCapacity);
}
elementData = Arrays.copyOf(elementData,newCpacity);
}
private static int hugeCapacity(int minCapacity){
if(minCapacity < 0){
throw new OutOfMemoryError;
}
return minCapacity > MAX_ARRAY_SIZE ?
Integer.MAX_VALUE:
MAX_ARRAY_SIZE;
}
Arrays.copyOf();
ArrayList ---- ADD2、3
// add(int index,E element)
public void add(int index,E element){
rangCheckForAdd(index);
ensureCapacityInternal(size+1);
System.arraycopy(elementData,index,elementData,index+1,size - index );
elementData[index] = element;
size++;
}
// add(Collection <? extends e> a)
public boolean add(Collection <? extends e> c){
Obeject[] a = c.toArray();
int newNum = a.length;
ensureCapacityInternal(size+newNum);
System.arraycopy(a,0,elementData,size,newNum);
size+=newNum;
return newNum != 0;
}
ArrayList ---- get
// get
public E get(int index){
rangcheck(index);
return elementData(index);
}
private void rangcheck(int index){
if(index >= 0){
thow new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
}
E elementData(int index){
return (E)elementData[index];
}
ArrayList ---- set
// set
public E set(int index,E element){
rangcheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
ArrayList ---- remove
// remove
public E remove(int index){
rangcheck(index);
E oldValue = elementData(index);
int newMoved = size - index - 1;
System.arraycopy(elementData,index+1,elementData,index,newMoved)
elementData[--size] = null;
return oldValue;
}
ArrayList是基于动态数组实现的,在增删时候,需要数组的拷贝复制。
ArrayList的默认初始化容量是10,每次扩容时候增加原先容量的一半,也就是变为原来的1.5倍
删除元素时不会减少容量,若希望减少容量则调用trimToSize()
它不是线程安全的。它能存放null值。