ArrayList collection elementData why not participate in serialization?

There are so in a piece of code in ArrayList 

  / ** 
     * ArrayList array of buffer memory elements. ArrayList capacity is the length of this array buffer. 
* When you add the first element, any empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA will be extended to DEFAULT_CAPACITY.
* / Transient Object [] elementData of; // non-nested class Private Access to Simplify

elementData is to store an array of all the elements of the current collection, but was transient keyword modification, transient indicates that the array does not participate in serialization.

If that happens, then the sequence of elements stored in the ArrayList not to lose it?

With this question then look down, and finally find such two methods to ArrayList  writeObject () and readObject ()

  / ** 
     * Save the state of ArrayList instance to a stream (i.e., the sequence of it). * / 
    Private  void the writeObject (java.io.ObjectOutputStream S)
         throws java.io.IOException {
         // write element count and any hidden is 
        int expectedModCount = ModCount; 
        s.defaultWriteObject (); // write out all of the current class non-static fields (non-static) and non-transient fields (non-transient) to the ObjectOutputStream

         // the size written to the ObjectOutputStream 
        s.writeInt (size); 

        // because ArrayList is scaleable, when adding elements, may expansion will, at this time there will be some unused space, so this way, to save space and reduce the time sequence of the 
        for ( int I = 0; I <size; I ++ ) {  // size stored in the array represent the number of elements
            s.writeObject (elementData [I]); // read out elementData ordered elements have been used to a stream 
        } 

        IF (ModCount =! expectedModCount) {
             the throw  new new a ConcurrentModificationException (); 
        } 
    }
  / ** 
     * ArrayList instance reconstructed (i.e., be deserialized) from the stream. 
     * / 
    Private  void the readObject (java.io.ObjectInputStream S)
         throws java.io.IOException, a ClassNotFoundException { 
        elementData of = EMPTY_ELEMENTDATA; 

        // read size, and all the hidden things 
        s.defaultReadObject (); 

        // read capacity 
        s. the readInt (); // ignored 

        IF (size> 0 ) {
             // as clone (), as allocated in accordance with the size of the array, rather than the capacity 
            ensureCapacityInternal (size); 

            Object [] a = elementData of;
             // read the correct order into all the elements. 
            for (int i=0; i<size; i++) {
                a[i] = s.readObject();
            }
        }
    }

 

Guess you like

Origin www.cnblogs.com/Deters/p/11287766.html