为什么ArrayList中使用临时存储? [英] Why does ArrayList use transient storage?

查看:131
本文介绍了为什么ArrayList中使用临时存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读Java的ArrayList的来源和我在其支持数组声明传来:

I was reading the source of Java's ArrayList and I came across its backing array declaration:

private transient Object[] elementData;

为什么这个需要是暂时的?为什么不能这个类被序列化?

Why does this need to be transient? Why can't this class be serialized?

感谢您的帮助!

推荐答案

它的可以的序列化;在的ArrayList 类只是需要照顾的事情本身,而不是使用默认的机制。看看在类中的的writeObject()的readObject()的方法,这是标准序列化机制的一部分。

It can be serialized; the ArrayList class just takes care of things itself, rather than using the default mechanism. Look at the writeObject() and readObject() methods in that class, which are part of the standard serialization mechanism.

如果你看看源,你会看到,而不是拯救整个阵列,可能会进行一些或多个null元素,只是实际的对象将被保存。这始终是更加高效,并允许的ArrayList 将在最佳的大小(即 elementData中重组,避免不必要的数组中过多的空白区域。)

If you look at the source, you see that rather than saving the whole array, possibly with some or many null elements, just the actual objects are saved. This is always more efficient, and allows the ArrayList to be reconstituted with elementData at an optimal size (i.e., without unnecessary excessive empty space in the array.)

这篇关于为什么ArrayList中使用临时存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆