ArrayList中elementData为什么被transient修饰?


原文:http://blog.csdn.net/zero__007/article/details/52166306


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

  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中的两个方法:
  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. }  
  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是一个缓存数组,它通常会预留一些容量,等容量不足时再扩充容量,那么有些空间可能就没有实际存储元素,采用上诉的方式来实现序列化时,就可以保证只序列化实际存储的那些元素,而不是整个数组,从而节省空间和时间。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值