简体   繁体   English

Groovy-List、ArrayList 和 Object Array 的区别

[英]Groovy- Difference between List, ArrayList and Object Array

I was looking to understand difference between groovy List , ArrayList and Object Array but couldn't find real (simple) examples.我想了解 groovy ListArrayListObject Array之间的区别,但找不到真正(简单)的例子。 Like, what can we do with Array , that can't be done with List or ArrayList ?比如,我们可以用Array做什么,而不能用ListArrayList做什么? I understand that Array is a fixed sequence of objects.我知道 Array 是一个固定的对象序列。 Just to mention that I've looked at this , this and this in java and trying to understand the points mentioned there.只是提一下,我已经在 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!谢谢你!

Yes, an Array is a data structure with a fixed size.是的, Array是具有固定大小的数据结构。 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. Array在运行时知道它的类型,试图在Array放入任何不合适的东西都会导致异常。

In Groovy, Arrays are not really idiomatic due to being low-level and inflexible (fixed-size).在 Groovy 中,由于低级和不灵活(固定大小),数组并不是真正惯用的。 They are supported for interoperation with Java.它们支持与 Java 的互操作。 Typically people using Groovy prefer List over Array .通常,使用 Groovy 的人更喜欢List不是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 确实尝试消除差异,例如,您可以在Array上使用size方法来获取元素的数量(即使在 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. java.util.List是一个接口,描述了由不同类型的列表实现的基本列表操作。 Lists use generic type parameters to describe what they can hold (with types being optional in Groovy).列表使用泛型类型参数来描述它们可以保存的内容(类型在 Groovy 中是可选的)。 The generic types on Lists are invariant, not covariant. Lists 上的泛型类型是不变的,而不是协变的。 Generic collections rely on compile-time checking to enforce type safety.泛型集合依赖编译时检查来强制类型安全。

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 LinkedList and When to use LinkedList<> over ArrayList<>?已经有几个问题了, ArrayList Vs LinkedList以及何时使用 LinkedList<> 而不是 ArrayList<>? , which cover the differences between LinkedList and ArrayList . ,其中涵盖了LinkedListArrayList之间的差异。

You can find differences between ArrayList and LinkedList , these are implementations of List (interface).您可以找到ArrayListLinkedList之间的差异,这些是List (接口)的实现。 Each implementation has different methods.每个实现都有不同的方法。 You can see these methods in:您可以在以下位置看到这些方法:

* Methods LinkedList * 方法链表

* Methods ArrayList * 方法 ArrayList

List can't be compared with ArrayList. List 无法与 ArrayList 进行比较。

List is an interface and ArrayList an implementation with certain characteristics. List是一个接口,而ArrayList是一个具有某些特性的实现。 Like all other programming languages, also Java has certain containers for certains problems.与所有其他编程语言一样,Java 也为某些问题提供了某些容器​​。 You can get the initial grasp here: http://docs.oracle.com/javase/1.5.0/docs/guide/collections/overview.html您可以在这里初步掌握: http : //docs.oracle.com/javase/1.5.0/docs/guide/collections/overview.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM