今天在看ArrayList源码,有如下困惑,希望得到大神们的解答。在ArrayList源码中定义了两个一模一样的数据结构,如下:private static final Object[] EMPTY_ELEMENTDATA = {};private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};官网给出的解释是这样子的:private static final Object[] EMPTY_ELEMENTDATA = {};Shared empty array instance used for empty instances.而,另外一个:private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};Shared empty array instance used for default sized empty instances. We distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when first element is added.求解答啊,谢谢~~~而且,本人吃惊的发现,DEFAULTCAPACITY_EMPTY_ELEMENTDATA 这个东西是JDK 1.8中才加进来的,我就更加神奇了,为什么JDK 1.8中要加入这个呢?
1 回答
月关宝盒
TA贡献1772条经验 获得超5个赞
We distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when first element is added.
我没读过源码, 但是我看到题主的注释里面有这么一句, 我们根据第一个元素被添加时知道多少个元素去填充把它与EMPTY_ELEMENTDATA区分开来.
添加回答
举报
0/150
提交
取消