第1部分 ArrayList介紹
ArrayList 是一個(gè)數(shù)組隊(duì)列,相當(dāng)于 動(dòng)態(tài)數(shù)組。與Java中的數(shù)組相比,它的容量能動(dòng)態(tài)增長(zhǎng)。它繼承于AbstractList,實(shí)現(xiàn)了List, RandomAccess, Cloneable, java.io.Serializable這些接口。
ArrayList 繼承了AbstractList,實(shí)現(xiàn)了List。它是一個(gè)數(shù)組隊(duì)列,提供了相關(guān)的添加、刪除、修改、遍歷等功能。
ArrayList 實(shí)現(xiàn)了RandmoAccess接口,即提供了隨機(jī)訪(fǎng)問(wèn)功能。RandmoAccess是java中用來(lái)被List實(shí)現(xiàn),為L(zhǎng)ist提供快速訪(fǎng)問(wèn)功能的。在ArrayList中,我們即可以通過(guò)元素的序號(hào)快速獲取元素對(duì)象;這就是快速隨機(jī)訪(fǎng)問(wèn)。稍后,我們會(huì)比較List的“快速隨機(jī)訪(fǎng)問(wèn)”和“通過(guò)Iterator迭代器訪(fǎng)問(wèn)”的效率。
ArrayList 實(shí)現(xiàn)了Cloneable接口,即覆蓋了函數(shù)clone(),能被克隆。
ArrayList 實(shí)現(xiàn)java.io.Serializable接口,這意味著ArrayList支持序列化,能通過(guò)序列化去傳輸。
和Vector不同,ArrayList中的操作不是線(xiàn)程安全的。所以,建議在單線(xiàn)程中才使用ArrayList,而在多線(xiàn)程中可以選擇Vector或者CopyOnWriteArrayList。
ArrayList的繼承關(guān)系
ArrayList與Collection關(guān)系如下圖:
ArrayList構(gòu)造函數(shù)
// 將當(dāng)前容量值設(shè)為 =實(shí)際元素個(gè)數(shù)
public void trimToSize() {
modCount++;
int oldCapacity = elementData.length;
if (size < oldCapacity) {
elementData = Arrays.copyOf(elementData, size);
}
}
// 確定ArrarList的容量。
// 若ArrayList的容量不足以容納當(dāng)前的全部元素,設(shè)置 新的容量=“(原始容量x3)/2 + 1”
public void ensureCapacity(int minCapacity) {
// 將“修改統(tǒng)計(jì)數(shù)”+1
modCount++;
int oldCapacity = elementData.length;
// 若當(dāng)前容量不足以容納當(dāng)前的元素個(gè)數(shù),設(shè)置 新的容量=“(原始容量x3)/2 + 1”
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
// 添加元素e
public boolean add(E e) {
// 確定ArrayList的容量大小
ensureCapacity(size + 1); // Increments modCount!!
// 添加e到ArrayList中
elementData[size++] = e;
return true;
}
// 返回ArrayList的實(shí)際大小
public int size() {
return size;
}
// 返回ArrayList是否包含Object(o)
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
// 返回ArrayList是否為空
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;
}
// 反向查找(從數(shù)組末尾向開(kāi)始查找),返回元素(o)的索引值
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;
}
// 返回ArrayList的Object數(shù)組
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
// 返回ArrayList的模板數(shù)組。所謂模板數(shù)組,即可以將T設(shè)為任意的數(shù)據(jù)類(lèi)型
public <T> T[] toArray(T[] a) {
// 若數(shù)組a的大小 < ArrayList的元素個(gè)數(shù);
// 則新建一個(gè)T[]數(shù)組,數(shù)組大小是“ArrayList的元素個(gè)數(shù)”,并將“ArrayList”全部拷貝到新數(shù)組中
if (a.length < size)
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
// 若數(shù)組a的大小 >= ArrayList的元素個(gè)數(shù);
// 則將ArrayList的全部元素都拷貝到數(shù)組a中。
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
// 獲取index位置的元素值
public E get(int index) {
RangeCheck(index);
return (E) elementData[index];
}
// 設(shè)置index位置的值為element
public E set(int index, E element) {
RangeCheck(index);
E oldValue = (E) elementData[index];
elementData[index] = element;
return oldValue;
}
// 將e添加到ArrayList中
public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
// 將e添加到ArrayList的指定位置
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
ensureCapacity(size+1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
// 刪除ArrayList指定位置的元素
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; // Let gc do its work
return oldValue;
}
// 刪除ArrayList的指定元素
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;
}
// 快速刪除第index個(gè)元素
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
// 從"index+1"開(kāi)始,用后面的元素替換前面的元素。
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
// 將最后一個(gè)元素設(shè)為null
elementData[--size] = null; // Let gc do its work
}
// 刪除元素
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
// 便利ArrayList,找到“元素o”,則刪除,并返回true。
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
// 清空ArrayList,將全部的元素設(shè)為null
public void clear() {
modCount++;
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
// 將集合c追加到ArrayList中
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacity(size + numNew); // Increments modCount
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
// 從index位置開(kāi)始,將集合c添加到ArrayList
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); // 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;
}
// 刪除fromIndex到toIndex之間的全部元素。
protected void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// Let gc do its work
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);
}
// 克隆函數(shù)
public Object clone() {
try {
ArrayList<E> v = (ArrayList<E>) super.clone();
// 將當(dāng)前ArrayList的全部元素拷貝到v中
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
}
// java.io.Serializable的寫(xiě)入函數(shù)
// 將ArrayList的“容量,所有的元素值”都寫(xiě)入到輸出流中
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
// Write out element count, and any hidden stuff
int expectedModCount = modCount;
s.defaultWriteObject();
// 寫(xiě)入“數(shù)組的容量”
s.writeInt(elementData.length);
// 寫(xiě)入“數(shù)組的每一個(gè)元素”
for (int i=0; i<size; i++)
s.writeObject(elementData[i]);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
// java.io.Serializable的讀取函數(shù):根據(jù)寫(xiě)入方式讀出
// 先將ArrayList的“容量”讀出,然后將“所有的元素值”讀出
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in size, and any hidden stuff
s.defaultReadObject();
// 從輸入流中讀取ArrayList的“容量”
int arrayLength = s.readInt();
Object[] a = elementData = new Object[arrayLength];
// 從輸入流中將“所有的元素值”讀出
for (int i=0; i<size; i++)
a[i] = s.readObject();
}
}
public static void iteratorThroughFor2(List list) {
long startTime;
long endTime;
startTime = System.currentTimeMillis();
for(Object obj:list)
endTime = System.currentTimeMillis();
long interval = endTime - startTime;
System.out.println("iteratorThroughFor2:" + interval+" ms");
}
}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注