[ Java ]ArrayList 里面 elementData 为什么要设成 transient

2019-05-27 15:35:17 +08:00
 MaxStack

看了下 ArrayList 的源码

    /**
    * The array buffer into which the elements of the ArrayList are stored.
    * The capacity of the ArrayList is the length of this array buffer. Any
    * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
    * will be expanded to DEFAULT_CAPACITY when the first element is added.
    */
    transient Object[] elementData; // non-private to simplify nested class access

看到 elementData 是 transient 的,于是又去看了下序列化的实现。

    /**
     * Save the state of the <tt>ArrayList</tt> instance to a stream (that
     * is, serialize it).
     *
     * @serialData The length of the array backing the <tt>ArrayList</tt>
     *             instance is emitted (int), followed by all of its elements
     *             (each an <tt>Object</tt>) 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 size as capacity for behavioural compatibility with clone()
        s.writeInt(size);

        // Write out all elements in the proper order.
        for (int i=0; i<size; i++) {
            s.writeObject(elementData[i]);
        }

        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
    }

我觉得这里没有必要把 elementData 设成 transient,然后再一个一个遍历输出。直接用 defaultWriteObject 把 elementData 输出就可以了。

我自己写了个 Dummy 的 ArrayList 测试,是可以直接序列化+反序列化数组的。

求大神指点。

1627 次点击
所在节点    Java
3 条回复
wdmx007
2019-05-27 17:41:48 +08:00
你看源码里面有类似于 fast-fail 的检查, 和 iterator.remove 类似,
当有其他线程修改这个数组时,能保证序列化最终写入的数组内容与开始调用这个函数时相同。
MaxStack
2019-05-27 17:51:39 +08:00
对的,你指的应该是 modCount 吧,如果有修改的话抛异常 ConcurrentModificationException。这部分是有用的。

我是觉得遍历数组依次输出这部分没必要。
~~~
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
~~~
SoloCompany
2019-05-27 20:32:40 +08:00
1. elementData 又不总是满的
2. 应尽量确保值相等的 array list 总是得到同样的序列化结果

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/568054

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX