- 类定义
public class ArrayList<E> extends AbstractList<E> implements List<E>,RandomAccess,Cloneable,java.io.Serializable
- 域
private static final int DEFAULT_CAPACITY=10;
private static final Object[] EMPTY_ELEMENTDATA={};
private transient Object[] elementData;
private int size;
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
- 构造方法
public ArrayList(int initialCapacity){
super();
if(initialCapacity<0)
throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);
this.elementData=new Object[initialCapacity];
}
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}
public ArrayList(Collection <? extends E> c){
elementData = c.toArray();
size=elementData.length;
if(elementData.getClass!=Object[].class){
elementData=Arrays.copayOf(elementData,size,Object[].class);
}
}
- 其他方法
public void trimToSize(){
modCount++;
if(size < elementData.length){
elementData = Arrays.copyOf(elmentData,size);
}
}
public ensureCapacity(int minCapacity){
int minExpand=(elementData!=EMPTY_ELEMENTDATA)?0:DEFAULT_CAPACITY;
if(minCapacity>minExpand)
ensureExplicitCapactiy(minCapacity);
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == 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;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
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(elementData[i].equals(o))return i;
}
return -1;
}
public Object clone() {
try {
@SuppressWarnings("unchecked")
ArrayList<E> v = (ArrayList<E>) super.clone();
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
public Object[] toArray(){
return Arrays.copyOf(elementData,size);
}
public <T> T[] toArray(T[] a) {
if (a.length < size)
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
E elementData(int index) {
return (E) elementData[index];
}
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
public E set(int index, E element){
rangeCheck(index);
E oldValue = elementData[index];
elementData[index]=element;
return oldValue;
}
public boolean add(E e){
ensureCapacityInternal(size+1);
elementData[size++]=e;
return true;
}
public E remove(int index){
rangeCheck(index);
checkForComodification();
E result = parent.remove(parentOffset + index);
this.modCount = parent.modCount;
this.size--;
return result;
}