为什么在 Java 中更喜欢使用列表而不是数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2391553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is it preferred to use Lists instead of Arrays in Java?
提问by Venkat
Many people and authors suggested to us to use list than array.
许多人和作者建议我们使用列表而不是数组。
List <Integer> list = new ArrayList<Integer>();
list.addElement(1);
....
What it is the reason behind it?
它背后的原因是什么?
回答by nicerobot
You should generally prefer to choose the right data structure for the job. You need to understand your task at hand as well as all the different options you have and how they compare in terms of iteration, and searching, and adding, removing, and inserting data. In general, you need to understand how the data structure accesses and manipulates memory and choose the best data structure based on how you anticipate your application will be used.
您通常应该更喜欢为工作选择正确的数据结构。您需要了解您手头的任务以及您拥有的所有不同选项,以及它们如何在迭代、搜索、添加、删除和插入数据方面进行比较。通常,您需要了解数据结构如何访问和操作内存,并根据您预期应用程序的使用方式选择最佳数据结构。
Obviously, it isn't always clear-cut. But you can understand the ideals for different data structures.
显然,它并不总是明确的。但是您可以理解不同数据结构的理想情况。
For example, purely static, fixed length data in which you'll only iterate, without a need for search, is ideal for an array. It's common to use such arrays in cipher algorithms. If the data is static but instead of iterating, you need to search, you might want some type of tree structure. If you want fast insertion, hashing is probably the ideal. If the data changes often, you want a structure that is efficient at changing its size, like a list.
例如,纯静态、固定长度的数据(您只需在其中迭代而不需要搜索)是数组的理想选择。在密码算法中使用这样的数组是很常见的。如果数据是静态的但不是迭代,您需要搜索,您可能需要某种类型的树结构。如果你想要快速插入,散列可能是理想的。如果数据经常更改,您需要一个能够有效更改其大小的结构,例如列表。
Of course, there's many variations and combinations of data structures designed to solve all kinds of specific problems. The reason there are so many is because of the importance they play in writing efficient programs. Anyway, my point is, learn about data structures. Understand the ideal situations for each and then you'll be able to decide or design suitable data structures for any task.
当然,有许多数据结构的变体和组合旨在解决各种特定问题。之所以有这么多,是因为它们在编写高效程序方面发挥着重要作用。无论如何,我的观点是,学习数据结构。了解每种情况的理想情况,然后您将能够为任何任务决定或设计合适的数据结构。
回答by Adriaan Stander
From Array vs ArrayList
An ArrayList is better than Array to use when you have no knowledge in advance about elements number. ArrayList are slower than Arrays. So, if you need efficiency try to use Arrays if possible.
当您事先不了解元素数量时,使用 ArrayList 比使用 Array 更好。ArrayList 比 Arrays 慢。因此,如果您需要效率,请尽可能尝试使用数组。
回答by Marius
Lists can easily grow in size, and you can add and remove elements in the middle of the list easily. That cannot be done with arrays. You need to consider what you need the list for though. If you don't think the list is going to change a lot, then use an array instead.
列表的大小很容易增长,您可以轻松地添加和删除列表中间的元素。这不能用数组来完成。不过,您需要考虑您需要该列表的用途。如果您认为列表不会发生很大变化,请改用数组。
回答by Kashif Q.
One thing to keep in mind is that the Java Collections classes favor general purpose ease of use over optimization for specific scenarios. So, as a previous responder said, you really need to consider how you're going to use it.
要记住的一件事是,Java Collections 类更喜欢通用的易用性,而不是针对特定场景的优化。因此,正如之前的响应者所说,您确实需要考虑如何使用它。
For example, if you're creating "large" data structures, then ArrayList can get pretty inefficient. Each time you hit the limit of the array, it allocates a new one at (I believe) 2x the size. So on average an ArrayList is only going to be 75% utilized.
例如,如果您正在创建“大型”数据结构,那么 ArrayList 可能会变得非常低效。每次达到数组的限制时,它都会以(我相信)2 倍的大小分配一个新数组。因此,平均而言,ArrayList 的利用率仅为 75%。
In general one can consider the Java Collections to be first approximations that are usually good enough most of the time, and when you have measurableperformance issues you should be prepared to use alternate, more specialized Collection implementations.
一般来说,人们可以将 Java 集合视为通常在大多数情况下足够好的第一近似值,并且当您遇到可衡量的性能问题时,您应该准备使用替代的、更专业的集合实现。
In the case you mention, you can consider ArrayList to be just a more convenient way to deal with an array.
在您提到的情况下,您可以将 ArrayList 视为处理数组的更方便的方法。
回答by Ledhund
I use Lists, ArrayLists et c mainly because I don't need to worry about where the next free slot is or if its large enough, since Sun already did that for me.
我使用 Lists、ArrayLists 等主要是因为我不需要担心下一个空闲插槽在哪里或者它是否足够大,因为 Sun 已经为我做了这些。
回答by Khaled Qasem
Yes, Declaring it as List is better than ArrayList. why? the answer is code decoupling.
是的,将其声明为 List 比 ArrayList 更好。为什么?答案是代码解耦。
The main reason to do this is to decouple you code from a specific implementation of the interface.
这样做的主要原因是将代码与接口的特定实现分离。
The rest of code only knows that data is type of List and hence you can switch between different implementations of the list interface.
其余代码只知道数据是列表类型,因此您可以在列表接口的不同实现之间切换。
Assume you declare it as an ArrayList and then you realize that you should have used LinkedList, changing it wouldn't be easy because there's no guarantee that the rest of code doesn't make use of methods specific to ArrayList.
假设您将其声明为 ArrayList,然后您意识到您应该使用 LinkedList,更改它并不容易,因为不能保证其余代码不使用特定于 ArrayList 的方法。
But if you declare it as List, they you can simply change instance of List from,
但是如果你将它声明为 List,你可以简单地改变 List 的实例,
List <Integer> list = new ArrayList<Integer>();
to
到
List <Integer> list = new LinkedList<Integer>();
and for sure this will work cuz you've written code that follow contract provided by list interface.
并且可以肯定这会起作用,因为您已经编写了遵循列表接口提供的合同的代码。
回答by Zaki
EDIT:
In certain cases when dealing with primitive types, its better to go with arrays because in the case of Arraylists, it involves boxing and unboxing of the primitives which might be a bit slower when compared to handling primitives with arrays.
编辑:
在处理原始类型的某些情况下,最好使用数组,因为在 Arraylists 的情况下,它涉及原始类型的装箱和拆箱,与使用数组处理原始类型相比,这可能会慢一些。

