ArrayList 与动态数组相比如何 [英] how does an ArrayList compare to a dynamic array

查看:29
本文介绍了ArrayList 与动态数组相比如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ArrayList 是否只是动态数组的接口?还是它们是同一回事?

Is an ArrayList is just the interface for a dynamic array? Or are they the same thing?

like:ArrayList对应动态数组,HashMap对应Map?

like: ArrayList corresponds to dynamic array, HashMap corresponds to Map ?

除了我没有看到任何用于动态数组之类的 Java API,除非它是 ArrayList?

except I don't see any Java API for something like a dynamic array, unless it is ArrayList?

在那种情况下,它们将是同一回事!

in that case they would be the same thing !

大概是同一个东西吧?

推荐答案

是的.简而言之.

更长的解释是,ArrayList 是使用数组进行存储的集合,而不是链表、双向链表或类似的.这意味着它提供了使用数组的所有好处,而 Java 会为您处理调整数组大小的机制(动态).

Yes. In short.

A longer explanation is that an ArrayList is a collection that uses arrays for storage, rather than a linked list, doubly linked list or similar. This means that it gives all the benefits of using an Array, whilst Java looks after the mechanics of sizing the Array for you (dynamically).

我好像记得初始数组是用默认的最大大小创建的(可以由用户指定).如果集合空间不足,则会创建一个更大的数组,并将原始数组的内容复制到新数组中.设置大小增量是为了防止这种情况经常发生,因为操作成本相当高.

I seem to remember that the initial array is created with a default maximum size (which can be specified by the user). Should the collection run out of space, then a larger array is created and the contents of the original array copied into the new one. The increment in size is set to prevent this happening too often, as the operation is fairly costly.

Java 还提供了类似的 Vector 集合,但也是线程安全的,请参阅:ArrayList 和 Vector 有什么区别?.

Java also offers the Vector collection which is similar, but is also thread safe, see: What are the differences between ArrayList and Vector?.

这篇关于ArrayList 与动态数组相比如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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