ArrayList源码分析

 
关键词 ArrayList源码分析                                          
  声明如下:
  public class ArrayList extends AbstractList   implements List, RandomAccess, Cloneable, java.io.Serializable
有关 AbstractList:http://blog.csdn.net/treeroot/archive/2004/09/14/104743.aspx
有关 List:     http://blog.csdn.net/treeroot/archive/2004/09/14/104638.aspx
有关 RandomAccess:http://blog.csdn.net/treeroot/archive/2004/09/14/104538.aspx
有关 Cloneable :http://blog.csdn.net/treeroot/archive/2004/09/07/96936.aspx
有关 java.io.Serializeable:  主要用于对象序列化。
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);
}
默认初始话内部数组大小为 10, 为什么是 10 是没有理由的,可能比较合适吧。
public ArrayList(Collection c) {
   size = c.size();
   // Allow 10% room for growth
   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;
   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) {
   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!!
   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() 提高效率。
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值