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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:18:59  来源:igfitidea点击:

Groovy- Difference between List, ArrayList and Object Array

javaarraysarraylistgroovy

提问by user1207289

I was looking to understand difference between groovy List, ArrayListand Object Arraybut couldn't find real (simple) examples. Like, what can we do with Array, that can't be done with Listor 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.

我一直在寻找,了解常规之间的差异ListArrayList并且Object Array也没有找到真正的(简单)的例子。比如,我们可以用 做什么Array,不能用List或做ArrayList什么?我知道 Array 是一个固定的对象序列。只是提一下,我已经在 java 中查看了thisthisthis并试图理解那里提到的要点。

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 Arrayis 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 Arrayknows its type at runtime and trying to put anything inappropriate in the Arraywill 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 Listover Array. Groovy does try to smooth out the differences, for instance you can use the sizemethod on an Arrayto get the number of elements (even though in Java you would have to use the lengthproperty).

在 Groovy 中,由于低级和不灵活(固定大小),数组并不是真正惯用的。它们支持与 Java 的互操作。通常使用Groovy的人喜欢ListArray。Groovy 确实尝试消除差异,例如您可以使用sizeanArray上的方法来获取元素的数量(即使在 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.Listis 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.ArrayListis 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 LinkedListand ArrayList.

已经有几个问题了,ArrayList Vs LinkedList以及何时使用 LinkedList<> 而不是 ArrayList<>?,其中涵盖了LinkedList和之间的差异ArrayList

回答by cfrick

Listis an interface and ArrayListan 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

ListArrayList具有某些特性的接口和实现。与所有其他编程语言一样,Java 也为某些问题提供了某些容器​​。您可以在这里初步掌握:http: //docs.oracle.com/javase/1.5.0/docs/guide/collections/overview.html

回答by Kevin F

You can find differences between ArrayListand LinkedList, these are implementations of List(interface). Each implementation has different methods. You can see these methods in:

您可以找到ArrayList和之间的差异LinkedList,这些是List(接口)的实现。每个实现都有不同的方法。您可以在以下位置看到这些方法:

*Methods LinkedList

*方法链表

*Methods ArrayList

*方法 ArrayList

List can't be compared with ArrayList.

List 无法与 ArrayList 进行比较。