ArrayList的elementData为什么要修饰为transient?
来源:互联网 发布:删除数据库 编辑:程序博客网 时间:2024/04/28 01:11
ArrayList源码:
private transient Object[] elementData;为什么要将elementData修饰为transient呢?
比较靠谱的说法:
elementData数组相当于容器,当容器不足时就会再扩充容量,但是容器的容量往往都是大于或者等于ArrayList所存元素的个数。
比如,现在实际有了8个元素,那么elementData数组的容量可能是8x1.5=12,如果直接序列化elementData数组,那么就会浪费4个元素的空间,特别是当元素个数非常多时,这种浪费是非常不合算的。所以ArrayList的设计者将elementData设计为transient,然后在writeObject方法中手动将其序列化,并且只序列化了实际存储的那些元素,而不是整个数组。
ArrayList覆写的writeObject()方法代码:
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{// Write out element count, and any hidden stuffint 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<size; i++) s.writeObject(elementData[i]);if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } }
可观察到循环时是使用i<size而不是i<elementData.length说明序列化时,只需实际存储的那些元素,而不是整个数组。
参考地址:http://www.cnblogs.com/aoguren/p/4767309.html
0 0
- ArrayList的elementData为什么要修饰为transient?
- ArrayList的elementData为什么要用transient修饰
- ArrayList中elementData为什么被transient修饰?
- ArrayList中为什么使用transient修饰elementData?
- ArrayList中elementData为什么被transient修饰?
- Arraylist的elementData修饰关键词transient到底要闹哪样
- ArrayList的elementData声明为transient,为什么还可以序列化
- java中ArrayList中为什么elementData使用了transient
- ArrayList中的数组为什么被transient修饰
- 用transient修饰的成员变量不能序列化,为什么ArrayList集合可以实现序列化
- java的ArrayList中,数组为什么被transient修饰,这是因为什么原因而设计出来的?
- transient:修饰符的使用
- 修饰符 transient的用法
- ArrayList的数组声明为transient却依旧序列化的原因
- 关于transient修饰符
- transient修饰符
- transient修饰符探索
- Java修饰符transient
- 随机森林算法入门(python)
- ActiveMQ的publish/subscribe
- Mysql命令大全
- 常见的网络设备
- 旅游预算
- ArrayList的elementData为什么要修饰为transient?
- linux中vim常用命令总结
- DateUtils的学习总结
- Android apk动态加载机制的研究(二):资源加载和activity生命周期管理
- 电子老鼠闯迷宫
- 解决哈希(HASH)冲突的主要方法
- Android下的沉浸式状态栏+折叠TitleBar(CoordinatorLayout+CollapsingToolbarLayout)+ViewPager切换实现
- 易语言如何识别文本是否全是数字
- Android Animation动画特效