当前位置:   article > 正文

java list序列化问题_java list序列化 | 学步园

java.util.list序列化失败

编图书馆的时候用了很多ArrayList,发现了反序列化后出现了如下问题:序列化前两个ArrayList中的某一元素指向同一对象,经过序列化再反序列化过程后,他们居然指向了不同对象,出现了不同步的问题,那是为什么呢?

查看一下Java.util里面的ArrayList源代码,发现ArrayList 重写了writeObject方法和readObject方法:

/**

* Save the state of the ArrayList instance to a stream (that

* is, serialize it).

*

* @serialData The length of the array backing the ArrayList

*             instance is emitted (int), followed by all of its elements

*             (each an Object) in the proper order.

*/

private void writeObject(java.io.ObjectOutputStream s)

throws java.io.IOException{

// Write out element count, and any hidden stuff

int expectedModCount = modCount;

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]);

if (modCount != expectedModCount) {

throw new ConcurrentModificationException();

}

}

/**

* Reconstitute the ArrayList instance from a stream (that is,

* deserialize it).

*/

private 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();

Object[] a = elementData = new Object[arrayLength];

// Read in all elements in the proper order.

for (int i=0; i

a[i] = s.readObject();

}

经过分析源代码以及实践证明:像Arraylist之类的引用序列化仍然是有效的,但是序列化之前ArrayList中指向同一对象的引用,经过序列化和反序列化之后,这个关系将被破坏,即他们不再指向同一个对象,而是指向了两个不同的对象。原因在于序列化的时候是直接把Arraylist中每个元素指向的对象写到文件中,反序列化时再一一读出。这点值得大家在编程的时候注意。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/767652
推荐阅读
相关标签
  

闽ICP备14008679号