ArrayList源碼分析

private static final long serialVersionUID = 8683452581122892189L;java

//版本控制程序員


private transient Object elementData[];算法

//內部結構,原來就是一個數組,這裏不讓它串行化。數組


private int size;安全

//該列表的大小,也就是元素個數。多線程


public ArrayList(int initialCapacity) {app

  super();ide

  if (initialCapacity < 0)函數

    throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);this

  this.elementData = new Object[initialCapacity];

}

//構造函數,初始化內部數組爲指定大小,注意列表是空的。


public ArrayList() {

  this(10);

}

//默認初始話內部數組大小爲10,爲何是10是沒有理由的,可能比較合適吧。


public ArrayList(Collection c) {

  size = c.size();

  // 增長10的容量

  elementData = new Object[(int)Math.min((size*110L)/100,Integer.MAX_VALUE)];

  c.toArray(elementData);

}

//經過一個集合來初始話該ArrayList,內部數組申請的空間比c大,主要是爲了提升效率。注意到c.toArray(elementData)

//方法的調用,這裏確定不會生成新的數組,若是用elementData=c.toArray()效率就差很多了。另外這裏調用了Math的靜態

//方法min來得到較小值。


public void trimToSize() {

  modCount++;

  int oldCapacity = elementData.length;

  if (size < oldCapacity) {

    Object oldData[] = elementData;

    elementData = new Object[size];

    System.arraycopy(oldData, 0, elementData, 0, size);

  }

}

//這個方法去掉多餘的空間,使內部數組的大小恰好等於ArrayList的size(),這個方法須要從新分配空間,而已須要一個數組

//拷貝過程(arraycopy是一個native方法,用的比較多),通常狀況下這個方法不多被調用。


public void ensureCapacity(int minCapacity) {

  modCount++;

//原始定義數組的長度

  int oldCapacity = elementData.length;

//minCapacity是最新數組長度,若是大於原始長度,就會擴容1.5倍+1

  if (minCapacity > oldCapacity) {

    Object oldData[] = elementData;

    int newCapacity = (oldCapacity * 3)/2 + 1;

    if (newCapacity < minCapacity)

      newCapacity = minCapacity;

//從新建立一個新數組

    elementData = new Object[newCapacity];

//將原始數組的數據拷貝到新數組中

    System.arraycopy(oldData, 0, elementData, 0, size);

  }

}

//這個方法來擴大ArrayList的容量,使它至少能容納minCapacity個元素,若是數組容量大於該值,什麼也不作。不然按某個

//算法(1.5倍加1)增長,若是不夠minCapacity大的話,就設置爲minCapacity。這個方法在add和addAll方法中都要調用,

//這裏爲何設置爲public呢?由於每次從新分配空間都是比較消耗時間的(new操做還要arrayCopy),若是能預計可能的大小

//的話,這個方法就有比較的靈活性。雖然該擴容算髮已經比較好,可是仍是能夠經過本身的控制提升效率,這個方法爲程序員

//帶來的方便。

//eg1:

ArrayList al=new ArrayList();

for(int i=0;i<100;i++){

  Object obj=new Object();

  al.add(obj);

}

//eg2(更高效):

ArrayList al=new ArrayList(100);

for(int i=0;i<100;i++){

  Object obj=new Object();

  al.add(obj);

}

//或者

ArrayList al=new ArrayList();

al.ensureCapacity(100);

for(int i=0;i<100;i++){

  Object obj=new Object();

  al.add(obj);

}


public int size() {

  return size;

}

//返回大小


public boolean isEmpty() {

  return size == 0;

}

//是否爲空


public boolean contains(Object elem) {

  return indexOf(elem) >= 0;

}

//是否包含指定元素,調用的是indexOf()方法。


public int indexOf(Object elem) {

  if (elem == null) {

    for (int i = 0; i < size; i++)

      if (elementData[i]==null)

        return i;

  } else {

    for (int i = 0; i < size; i++)

      if (elem.equals(elementData[i]))

        return i;

  }

  return -1;

}

//這個方法遍歷列表(數組0..size-1)


public int lastIndexOf(Object elem) {

  if (elem == 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 (elem.equals(elementData[i]))

        return i;

  }

  return -1;

}

//這個方法和上面的基本同樣,順序不同而已。


public Object clone() {

  try {

    ArrayList v = (ArrayList)super.clone();

    v.elementData = new Object[size];

    System.arraycopy(elementData, 0, v.elementData, 0, size);

    v.modCount = 0;

    return v;

  } catch (CloneNotSupportedException e) {

  // this shouldn't happen, since we are Cloneable

    throw new InternalError();

  }

} 

//覆蓋Object中的clone()方法,實現clone,注意這裏是一個淺拷貝,兩個ArrayList中的數組中的元素

//是相同的,由於System.arraycopy就是淺拷貝。


public Object[] toArray() {

  Object[] result = new Object[size];

  System.arraycopy(elementData, 0, result, 0, size);

  return result;

}

//返回ArrayList元素的一個數組,注意這裏雖然生成了一個新的數組,可是數組元素和集合中的元素是共享的,

//Collection接口中說這個是安全的是不嚴格的,下面的例子演示了這個效果。

//eg1:

ArrayList al=new ArrayList();

al.add(new StringBuffer("hello"));

Object[] a=al.toArray();

StringBuffer sb=(StringBuffer)a[0];

sb.append("changed");         //改變數組元素一樣也改變了原來的ArrayList中的元素

System.out.println(al.get(0));    

//這裏不要用String來代替StringBuffer,由於String是常量。


public Object[] toArray(Object a[]) {

  if (a.length < size)

    a = (Object[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);

   System.arraycopy(elementData, 0, a, 0, size);

   if (a.length > size)

      a[size] = null;

   return a;

}

//這個方法有可能不須要生成新的數組,注意到若是數組a容量過大,只在size處設置爲null。


public Object get(int index) {

  RangeCheck(index);

  return elementData[index];

}

//能夠看隨機訪問效率是很高的,和數組的索引訪問是同樣的,方式設計到索引值都會先檢查。


public Object set(int index, Object element) {

  RangeCheck(index);

   Object oldValue = elementData[index];

  elementData[index] = element;

  return oldValue;

}

//更新指定位置的值,並訪問原來的值。


public boolean add(Object o) {

//擴大集合容量,容量加1

  ensureCapacity(size + 1); // Increments modCount!!

//添加一個新的元素到末尾

  elementData[size++] = o;

  return true;

}

//添加一個新的元素,前面說道新增方法都要先調用ensureCapacity方法,這裏沒有調用add(size,o)方法。


public void add(int index, Object element) {

  if (index > size || index < 0)

   throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);

   ensureCapacity(size+1); // Increments modCount!!

//elementData:原數組

   //index:原數組開始位置

   //elementData:目標數組

   //index+1:目標數組開始位置

   //size-index:拷貝的長度

  System.arraycopy(elementData, index, elementData, index + 1,size - index);

  elementData[index] = element;

  size++;

}

//在指定位置插入元素,指定元素和後面的元素後移。


public Object remove(int index) {

  RangeCheck(index);

   modCount++;

  Object oldValue = 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;

}

//刪除指定位置的元素,後面的元素前移,返回被刪除的元素,這裏要注意的elementData[--size]=null這條語句,

//若是不這樣的話,就有可能形成內存泄露,由於該對象其實已經沒有用,可是內部還有一個它的引用,若是不設置

//爲null,GC就沒法回收這個空間,積累多了就有可能形成內存泄露,這裏只說有可能,而不是必定。


public void clear() {

  modCount++;

  // Let gc do its work

  for (int i = 0; i < size; i++)

    elementData[i] = null;

   size = 0;

}

//這段代碼比較高效,這裏也必須設置爲空引用,理由同上。


public boolean addAll(Collection c) {

  modCount++;

  int numNew = c.size();

  ensureCapacity(size + numNew);

   Iterator e = c.iterator();

  for (int i=0; i    elementData[size++] = e.next();

   return numNew != 0;

}

//添加集合c中的元素到ArrayList的末尾,添加成功返回true,若是集合c爲空,返回false。


public boolean addAll(int index, Collection c) {

  if (index > size || index < 0)

    throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);

   int numNew = c.size();

  ensureCapacity(size + numNew); // Increments modCount!!

   int numMoved = size - index;

   if (numMoved > 0)

      System.arraycopy(elementData, index, elementData, index + numNew,numMoved);

   Iterator e = c.iterator();

   for (int i=0; i    elementData[index++] = e.next();

   size += numNew;

   return numNew != 0;

}

//在指定位置插入集合中的全部元素,和上面一個方法基本差很少,指定位置元素和之後的都要後移。


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;

}

//這是一個保護方法,刪除指定位置fromIndex到toIndex的元素,包括前面不包括後面。


private void RangeCheck(int index) {

  if (index >= size || index < 0)

    throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);

}

//不用解釋。


private synchronized void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{

  // Write out element count, and any hidden stuff

  s.defaultWriteObject();

   // Write out array length

  s.writeInt(elementData.length);

   // Write out all elements in the proper order.

  for (int i=0; i    s.writeObject(elementData[i]);

}

private synchronized void readObject(java.io.ObjectInputStream s) throws               java.io.IOException,ClassNotFoundException {

  // Read in size, and any hidden stuff

  s.defaultReadObject();

  // Read in array length and allocate array

  int arrayLength = s.readInt();

  elementData = new Object[arrayLength];

   // Read in all elements in the proper order.

  for (int i=0; i    elementData[i] = s.readObject();

}

//這兩個方法是爲了實現串行化而寫的,這裏要注意幾點:

//1.這兩個方法並非java.io.Serializable中定義的方法,Serializable只是標記接口。

//2.串行化對象的時候會先檢查該對象是否實現了這兩個方法,若是有實現就調用他們,若是沒有的化就調用默認方法。

//至於爲何能夠調用該對象的private方法我也不清楚,這個問題確實比較奇怪,若是能夠訪問對象的private方法,那

//也太不安全了。

//3.由於elementData聲明爲transient,因此必須手動串行化化。


//總結:

//1.ArrayList的方法都沒有同步,因此在多線程中是不安全的,必須本身同步,並且ArrayList不少方法聲明對於多線程操做

//都沒有規定,就是說結果不可預料。

//2.toArray()方法返回的是和原列表相同的對象,也就是說:

arrayList.toArray()[0]==arrayList.get(0)//返回的是true(假定arrayList不空)。

//3.clone()方法是一個淺拷貝。

//4.能夠經過本身調用ensureCapacity()提升效率。

相關文章
相關標籤/搜索