ArrayList 是如何工作的? [英] How does ArrayList work?

查看:27
本文介绍了ArrayList 是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ArrayList 内部使用什么数据结构?

What data structure does an ArrayList use internally?

推荐答案

ArrayList 在内部使用 Object[].

当您向 ArrayList 添加项目时,列表会检查后备数组是否还有剩余空间.如果有空间,则新项目将添加到下一个空白空间.如果没有空间,则会创建一个新的更大的数组,并将旧数组复制到新数组中.

As you add items to an ArrayList, the list checks to see if the backing array has room left. If there is room, the new item is just added at the next empty space. If there is not room, a new, larger, array is created, and the old array is copied into the new one.

现在,还有更多的空间,新元素被添加到下一个空白空间.

Now, there is more room left, and the new element is added in the next empty space.

因为人们真的很喜欢源代码:

Since people really like the source code:

/**
 * The array buffer into which the elements of the ArrayList are stored.
 * The capacity of the ArrayList is the length of this array buffer.
 */
private transient Object[] elementData;

直接脱离 JDK.

这篇关于ArrayList 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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