阵列的优势 [英] Benefits of arrays
问题描述
在我看来,一个列表中对数组的优点是pretty显而易见的:
As I see it, the advantages of a list over an array are pretty obvious:
- 泛型提供更precise打字:
列表<整数>中名单,LT ;?扩展号码>中名单,LT ;?超级整数方式>
- 一个List接口有一堆有用的方法:
的addAll
,删除
等,虽然对数组所有标准操作,除了获取/设置必须以程序的方式将它传递给一个静态方法来进行。 - 集合提供诸如
的ArrayList
,的LinkedList
,unmodifieable和同步列表,它可以在一个共同的隐藏不同的实现List接口。 - OOB长度控制。
- Generics provide more precise typing:
List<Integer>, List<? extends Number>, List<? super Integer>
. - A List interface has a bunch useful methods:
addAll
,remove
etc. While for arrays all standard operations except get/set must be performed in a procedure manner by passing it to a static method. - Collections offer different implementations like
ArrayList
,LinkedList
, unmodifieable and synchronized lists, which can be hidden under a common List interface. - OOB length control.
至于缺点,我只能提没有语法糖和运行时类型检查。同时支持这两种结构的需要频繁使用的 asList
和的toArray
方法,这使得code少可读。所以,我如果有使用我想念阵列中的任何重要的好处好奇。
As disadvantages I can only mention the absence of syntactic sugar and a runtime type check. At the same time supporting of both structures requires frequent using of asList
and toArray
methods, which makes code less readable. So I am curious if there are any important benefits of using arrays that I miss.
推荐答案
数组是更有效的,无论是在处理时间和内存占用方面。这尤其适用,如果你是在原始的类型,如操作 INT
或长
,因为名单
要求所有的元素被包裹在一个对象
(如整数
或龙
)。同时通过Java 5中引入的自动装箱功能降低code你需要这样的包装和展开的数量,它不会删除的性能问题,因为仍在创建包装对象。
Arrays are more efficient, both in terms of processing time and memory footprint. This particularly applies if you are operating on primitive types, such as int
or long
, since List
requires all elements to be wrapped in an Object
(such as Integer
or Long
). While the autoboxing features introduced by Java 5 reduces the amount of code you need for such wrapping and unwrapping, it does not remove the performance issues, as wrapper objects are still being created.
然而,大多数应用程序可能没有与这些问题有关的任何性能瓶颈,所以在大多数情况下,列表
等收藏应该做的罚款。在这些情况下,易于编程的远远超过内存或CPU使用率,和列表
的增加是正确的选择。
However, most applications probably do not have any performance bottlenecks related to these issues, so in most cases, List
and other collections should do fine. In these cases, the ease of programming outweighs the increase in memory or CPU usage, and List
is the right choice.
这篇关于阵列的优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!