Java Groovy-List、ArrayList 和 Object Array 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28483640/
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
Groovy- Difference between List, ArrayList and Object Array
提问by user1207289
I was looking to understand difference between groovy List
, ArrayList
and Object Array
but couldn't find real (simple) examples. Like, what can we do with Array
, that can't be done with List
or ArrayList
? I understand that Array is a fixed sequence of objects. Just to mention that I've looked at this, thisand thisin java and trying to understand the points mentioned there.
我一直在寻找,了解常规之间的差异List
,ArrayList
并且Object Array
也没有找到真正的(简单)的例子。比如,我们可以用 做什么Array
,不能用List
或做ArrayList
什么?我知道 Array 是一个固定的对象序列。只是提一下,我已经在 java 中查看了this、this和this并试图理解那里提到的要点。
I hope I am describing my issue clearly, but let me know if I am not clear or totally missing the point. Can someone point me to the right direction? Thank You!
我希望我能清楚地描述我的问题,但如果我不清楚或完全没有抓住重点,请告诉我。有人可以指出我正确的方向吗?谢谢你!
采纳答案by Nathan Hughes
Yes, an Array
is a data structure with a fixed size. It is declared as having a type that describes what elements it can hold, that type is covariant (see here for covariant vs contravariant). The Array
knows its type at runtime and trying to put anything inappropriate in the Array
will result in an exception.
是的,anArray
是一个固定大小的数据结构。它被声明为具有描述它可以容纳哪些元素的类型,该类型是协变的(有关协变与逆变,请参见此处)。该Array
知道它在运行时类型,并试图把任何的不当Array
会导致异常。
In Groovy, Arrays are not really idiomatic due to being low-level and inflexible (fixed-size). They are supported for interoperation with Java. Typically people using Groovy prefer List
over Array
. Groovy does try to smooth out the differences, for instance you can use the size
method on an Array
to get the number of elements (even though in Java you would have to
use the length
property).
在 Groovy 中,由于低级和不灵活(固定大小),数组并不是真正惯用的。它们支持与 Java 的互操作。通常使用Groovy的人喜欢List
过Array
。Groovy 确实尝试消除差异,例如您可以使用size
anArray
上的方法来获取元素的数量(即使在 Java 中您必须使用该length
属性)。
(In Ruby the data structure most closely resembling a list is called Array
, so people coming to Groovy or Grails from Rails without a Java background tend to carry over the nomenclature with resulting confusion.)
(在 Ruby 中,最类似于列表的数据结构称为Array
,因此没有 Java 背景的从 Rails 转到 Groovy 或 Grails 的人往往会沿用命名法,从而导致混淆。)
java.util.List
is an interface that describes basic list operations that are implemented by the different kinds of Lists. Lists use generic type parameters to describe what they can hold (with types being optional in Groovy). The generic types on Lists are invariant, not covariant. Generic collections rely on compile-time checking to enforce type safety.
java.util.List
是一个接口,描述了由不同类型的列表实现的基本列表操作。列表使用泛型类型参数来描述它们可以保存的内容(类型在 Groovy 中是可选的)。Lists 上的泛型类型是不变的,而不是协变的。泛型集合依赖编译时检查来强制类型安全。
In Groovy when you create a list using the literal syntax (def mylist = []
) the java.util.ArrayList
is the implementation you get:
在 Groovy 中,当您使用文字语法 ( def mylist = []
)创建列表时,您java.util.ArrayList
会得到以下实现:
groovy:000> list = ['a', 'b', 'c']
===> []
groovy:000> list instanceof List
===> true
groovy:000> list.class
===> class java.util.ArrayList
groovy:000> list.class.array
===> false
groovy:000> list << 'd'
===> [d]
groovy:000> list[0]
===> a
In order to create an array you have to add as (type)[]
to the declaration:
为了创建一个数组,你必须添加as (type)[]
到声明中:
groovy:000> stringarray = ['a', 'b', 'c'] as String[]
===> [a, b, c]
groovy:000> stringarray.class
===> class [Ljava.lang.String;
groovy:000> stringarray.class.array
===> true
groovy:000> stringarray << 'd'
ERROR groovy.lang.MissingMethodException:
No signature of method: [Ljava.lang.String;.leftShift() is applicable
for argument types: (java.lang.String) values: [d]
groovy:000> stringarray[0]
===> a
There are already several questions, ArrayList Vs LinkedListand When to use LinkedList<> over ArrayList<>?, which cover the differences between LinkedList
and ArrayList
.
已经有几个问题了,ArrayList Vs LinkedList以及何时使用 LinkedList<> 而不是 ArrayList<>?,其中涵盖了LinkedList
和之间的差异ArrayList
。
回答by cfrick
List
is an interface and ArrayList
an implementation with certain characteristics. Like all other programming languages, also Java has certain containers for certains problems. You can get the initial grasp here: http://docs.oracle.com/javase/1.5.0/docs/guide/collections/overview.html
List
是ArrayList
具有某些特性的接口和实现。与所有其他编程语言一样,Java 也为某些问题提供了某些容器。您可以在这里初步掌握:http: //docs.oracle.com/javase/1.5.0/docs/guide/collections/overview.html
回答by Kevin F
You can find differences between ArrayList
and LinkedList
, these are implementations of List
(interface). Each implementation has different methods. You can see these methods in:
您可以找到ArrayList
和之间的差异LinkedList
,这些是List
(接口)的实现。每个实现都有不同的方法。您可以在以下位置看到这些方法:
*方法链表
List can't be compared with ArrayList.
List 无法与 ArrayList 进行比较。