ArrayListpublic class ArrayList extends AbstractList implements RandomAccess
Fields Summary |
---|
private E[] | arr | private int | size |
Constructors Summary |
---|
public ArrayList(int cap)
if (cap < 0)
throw new IllegalArgumentException("Illegal Capacity: "+cap);
arr = (E[])new Object[cap]; // unchecked
| public ArrayList() this(10);
| public ArrayList(Collection c)
this(c.size()); addAll(c);
|
Methods Summary |
---|
public void | add(int i, E elt)
checkBounds(i,size+1);
ensureCapacity(size+1);
System.arraycopy(arr,i,arr,i+1,size-i);
arr[i] = elt; size++;
| private void | checkBounds(int i, int size)
if (i < 0 || i >= size)
throw new IndexOutOfBoundsException("Index: "+i+", Size: "+size);
| public void | ensureCapacity(int mincap)
int oldcap = arr.length;
if (mincap > oldcap) {
int newcap = Math.max(mincap, (oldcap*3)/2+1);
E[] oldarr = arr;
arr = (E[])new Object[newcap]; // unchecked cast
System.arraycopy(oldarr,0,arr,0,size);
}
| public E | get(int i)
checkBounds(i,size); return arr[i];
| public E | remove(int i)
checkBounds(i,size);
E old = arr[i]; arr[i] = null; size--;
System.arraycopy(arr,i+1,arr,i,size-i);
return old;
| public E | set(int i, E elt)
checkBounds(i,size); E old = arr[i]; arr[i] = elt; return old;
| public int | size() return size;
| public T[] | toArray(T[] newarr)
if (newarr.length < size) {
newarr = GenericArray.newInstance(newarr, size);
}
System.arraycopy(arr,0,newarr,0,size);
if (size < newarr.length) newarr[size] = null;
return newarr;
|
|