欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

ArrayList 原理、 扩容机制,arraylist扩容

来源: javaer 分享于  点击 40407 次 点评:277

ArrayList 原理、 扩容机制,arraylist扩容


ArrayList中elementData为什么被transient修饰?

       我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的过程,只要这个类实现了Serilizable接口,这个的所有属性和方法都会自动序列化。

        然而在实际开发过程中,我们常常会遇到这样的问题,这个类的有些属性需要序列化,而其他属性不需要被序列化。java的transient关键字为我们提供了便利,你只需要实现Serilizable接口,将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会序列化到指定的目的地中。

 Java的ArrayList中,定义了一个数组elementData用来装载对象的,具体定义如下:

[java] view plain copy
  1. /** 
  2.  * The array buffer into which the elements of the ArrayList are stored. 
  3.  * The capacity of the ArrayList is the length of this array buffer. Any 
  4.  * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA 
  5.  * will be expanded to DEFAULT_CAPACITY when the first element is added. 
  6.  */  
  7. transient Object[] elementData; // non-private to simplify nested class access  
       transient用来表示一个域不是该对象序行化的一部分,当一个对象被序行化的时候,transient修饰的变量的值是不包括在序行化的表示中的。但是ArrayList又是可序行化的类,elementData是ArrayList具体存放元素的成员,用transient来修饰elementData,岂不是反序列化后的ArrayList丢失了原先的元素?
       其实玄机在于ArrayList中的两个方法
[java] view plain copy
  1. /** 
  2.  * Save the state of the <tt>ArrayList</tt> instance to a stream (that 
  3.  * is, serialize it). 
  4.  * 
  5.  * @serialData The length of the array backing the <tt>ArrayList</tt> 
  6.  *             instance is emitted (int), followed by all of its elements 
  7.  *             (each an <tt>Object</tt>) in the proper order. 
  8.  */  
  9. private void writeObject(java.io.ObjectOutputStream s)  
  10.     throws java.io.IOException{  
  11.     // Write out element count, and any hidden stuff  
  12.     int expectedModCount = modCount;  
  13.     s.defaultWriteObject();  
  14.   
  15.     // Write out size as capacity for behavioural compatibility with clone()  
  16.     s.writeInt(size);  
  17.   
  18.     // Write out all elements in the proper order.  
  19.     for (int i=0; i<size; i++) {  
  20.         s.writeObject(elementData[i]);  
  21.     }  
  22.   
  23.     if (modCount != expectedModCount) {  
  24.         throw new ConcurrentModificationException();  
  25.     }  
  26. }  
[java] view plain copy
  1. /** 
  2.  * Reconstitute the <tt>ArrayList</tt> instance from a stream (that is, 
  3.  * deserialize it). 
  4.  */  
  5. private void readObject(java.io.ObjectInputStream s)  
  6.     throws java.io.IOException, ClassNotFoundException {  
  7.     elementData = EMPTY_ELEMENTDATA;  
  8.   
  9.     // Read in size, and any hidden stuff  
  10.     s.defaultReadObject();  
  11.   
  12.     // Read in capacity  
  13.     s.readInt(); // ignored  
  14.   
  15.     if (size > 0) {  
  16.         // be like clone(), allocate array based upon size not capacity  
  17.         ensureCapacityInternal(size);  
  18.   
  19.         Object[] a = elementData;  
  20.         // Read in all elements in the proper order.  
  21.         for (int i=0; i<size; i++) {  
  22.             a[i] = s.readObject();  
  23.         }  
  24.     }  
  25. }  
       ArrayList在序列化的时候会调用writeObject,直接将size和element写入ObjectOutputStream;反序列化时调用readObject,从ObjectInputStream获取size和element,再恢复到elementData。
       为什么不直接用elementData来序列化,而采用上诉的方式来实现序列化呢?原因在于elementData是一个缓存数组,它通常会预留一些容量,等容量不足时再扩充容量,那么有些空间可能就没有实际存储元素,采用上诉的方式来实现序列化时,就可以保证只序列化实际存储的那些元素,而不是整个数组,从而节省空间和时间。

相关文章

    暂无相关文章

用户点评