ArrayList简介
ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长,类似于C语言中的动态申请内存,动态增长内存。
ArrayList不是线程安全的,只能用在单线程环境下,多线程环境下可以考虑用Collections.synchronizedList(List l)函数返回一个线程安全的ArrayList类,也可以使用concurrent并发包下的CopyOnWriteArrayList类。
ArrayList实现了Serializable接口,因此它支持序列化,能够通过序列化传输,实现了RandomAccess接口,支持快速随机访问,实际上就是通过下标序号进行快速访问,实现了Cloneable接口,能被克隆。
ArrayList源码剖析
ArrayList的源码如下(加入了比较详细的注释):
-
package java.util;
-
-
public class ArrayList extends AbstractList
-
implements List, RandomAccess, Cloneable, java.io.Serializable
-
{
-
-
private static final long serialVersionUID = 8683452581122892189L;
-
-
-
private transient Object[] elementData;
-
-
-
private int size;
-
-
-
public ArrayList(int initialCapacity) {
-
super();
-
if (initialCapacity < 0)
-
throw new IllegalArgumentException("Illegal Capacity: "+
-
initialCapacity);
-
-
this.elementData = new Object[initialCapacity];
-
}
-
-
-
public ArrayList() {
-
this(10);
-
}
-
-
-
public ArrayList(Collection extends E> c) {
-
elementData = c.toArray();http://www.huiyi8.com/jiaoben/
-
size = elementData.length;
-
if (elementData.getClass() != Object[].class)
-
elementData = Arrays.copyOf(elementData, size, Object[].class);
-
}
-
-
-
-
public void trimToSize() {
-
modCount++;
-
int oldCapacity = elementData.length;
-
if (size < oldCapacity) {
-
elementData = Arrays.copyOf(elementData, size);
-
}
-
}
-
-
-
-
-
public void ensureCapacity(int minCapacity) {
-
-
modCount++;
-
int oldCapacity = elementData.length;
-
-
if (minCapacity > oldCapacity) {
-
Object oldData[] = elementData;
-
int newCapacity = (oldCapacity * 3)/2 + 1;
-
-
if (newCapacity < minCapacity)
-
newCapacity = minCapacity;
-
elementData = Arrays.copyOf(elementData, newCapacity);
-
}
-
}
-
-
-
public boolean add(E e) {
-
-
ensureCapacity(size + 1);
-
-
elementData[size++] = e;
-
return true;
-
}
-
-
-
public int size() {
-
return size;
-
}
-
-
-
public boolean contains(Object o) {
-
return indexOf(o) >= 0;
-
}
-
-
-
public boolean isEmpty() {
-
return size == 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 (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 (o.equals(elementData[i]))
-
return i;
-
}
-
return -1;
-
}
-
-
-
-
public Object[] toArray() {
-
return Arrays.copyOf(elementData, size);
-
}
-
-
-
public 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;
-
}
-
-
-
public E get(int index) {
-
RangeCheck(index);
-
-
return (E) elementData[index];
-
}
-
-
-
public E set(int index, E element) {
-
RangeCheck(index);
-
-
E oldValue = (E) elementData[index];
-
elementData[index] = element;
-
return oldValue;
-
}
-
-
-
public boolean add(E e) {
-
ensureCapacity(size + 1);
-
elementData[size++] = e;
-
return true;
-
}
-
-
-
public void add(int index, E element) {
-
if (index > size || index < 0)
-
throw new IndexOutOfBoundsException(
-
"Index: "+index+", Size: "+size);
-
-
ensureCapacity(size+1);
-
System.arraycopy(elementData, index, elementData, index + 1,
-
size - index);
-
elementData[index] = element;
-
size++;
-
}
-
-
-
public E remove(int index) {
-
RangeCheck(index);
-
-
modCount++;
-
E oldValue = (E) elementData[index];
-
-
int numMoved = size - index - 1;
-
if (numMoved > 0)
-
System.arraycopy(elementData, index+1, elementData, index,
-
numMoved);
-
elementData[--size] = null;
-
-
return oldValue;
-
}
-
-
-
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;
-
}
-
-
-
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;
-
}
-
-
-
public void clear() {
-
modCount++;
-
-
for (int i = 0; i < size; i++)
-
elementData[i] = null;
-
-
size = 0;
-
}
-
-
-
public boolean addAll(Collection extends E> c) {
-
Object[] a = c.toArray();
-
int numNew = a.length;
-
ensureCapacity(size + numNew);
-
System.arraycopy(a, 0, elementData, size, numNew);
-
size += numNew;
-
return numNew != 0;
-
}
-
-
-
public boolean addAll(int index, Collection extends E> c) {
-
if (index > size || index < 0)
-
throw new IndexOutOfBoundsException(
-
"Index: " + index + ", Size: " + size);
-
-
Object[] a = c.toArray();
-
int numNew = a.length;
-
ensureCapacity(size + numNew);
-
-
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;
-
}
-
-
-
protected void removeRange(int fromIndex, int toIndex) {
-
modCount++;
-
int numMoved = size - toIndex;
-
System.arraycopy(elementData, toIndex, elementData, fromIndex,
-
numMoved);
-
-
-
int newSize = size - (toIndex-fromIndex);
-
while (size != newSize)
-
elementData[--size] = null;
-
}
-
-
private void RangeCheck(int index) {
-
if (index >= size)
-
throw new IndexOutOfBoundsException(
-
"Index: "+index+", Size: "+size);
-
}
-
-
-
-
public Object clone() {
-
try {
-
ArrayList v = (ArrayList) super.clone();
-
-
v.elementData = Arrays.copyOf(elementData, size);
-
v.modCount = 0;
-
return v;
-
} catch (CloneNotSupportedException e) {
-
-
throw new InternalError();
-
}
-
}
-
-
-
-
-
private void writeObject(java.io.ObjectOutputStream s)
-
throws java.io.IOException{
-
-
int expectedModCount = modCount;
-
s.defaultWriteObject();
-
-
-
s.writeInt(elementData.length);
-
-
-
for (int i=0; i
-
s.writeObject(elementData[i]);
-
-
if (modCount != expectedModCount) {
-
throw new ConcurrentModificationException();
-
}
-
-
}
-
-
-
-
-
private void readObject(java.io.ObjectInputStream s)
-
throws java.io.IOException, ClassNotFoundException {
-
-
s.defaultReadObject();
-
-
-
int arrayLength = s.readInt();
-
Object[] a = elementData = new Object[arrayLength];
-
-
-
for (int i=0; i
-
a[i] = s.readObject();
-
}
-
}
阅读(371) | 评论(0) | 转发(0) |