Groovy - List,ArrayList和Object Array之间的区别 [英] Groovy- Difference between List, ArrayList and Object Array

查看:127
本文介绍了Groovy - List,ArrayList和Object Array之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望了解groovy List ArrayList Object Array 但找不到真实(简单)的例子。就像我们可以用 Array 做什么,这不能用 List 来完成。的ArrayList ?我明白Array是一个固定的对象序列。只要提到我已经看过 this this 在Java中,并试图理解这里提到的几点。 / p>

我希望我能清楚地描述我的问题,但是如果我不清楚或完全忽略这一点,请告诉我。有人能指引我走向正确的方向吗?谢谢!

解决方案

是的,数组是一个固定大小的数据结构。它被声明为描述哪些元素可以容纳的类型,该类型是协变的(在这里看到covariant vs contravariant )。该数组在运行时知道它的类型,并试图将任何不适当的东西放在数组中将导致异常。



在Groovy中,由于低级别和不灵活的(固定大小),它们支持与Java互操作。通常,使用Groovy的人更喜欢列出数组。 Groovy确实试图消除这些差异,例如,您可以在数组上使用size方法来获取元素的数量,即使在Java中,您也必须使用长度属性



(在Ruby中,与列表最接近的数据结构称为数组,因此在没有Java背景的情况下从Rails进入Groovy或Grails的人往往会因为混淆导致命名)。



java.util.List是一个接口,它描述了由不同种类的列表实现的基本列表操作。列表使用泛型类型参数来描述它们可以容纳的类型(Groovy中的类型是可选的)。由于类型擦除,列表不是协变的。在Groovy中,当您使用文字语法创建列表时( def mylist = [] / code>)java.util.ArrayList是您得到的实现:

  groovy:000> list = ['a','b','c'] 
===> []
groovy:000> list instanceof List
===> true
groovy:000> list.class
===>类java.util.ArrayList
groovy:000> list.class.array
===> false
groovy:000>列表<< 'd'
===> [d]
常规:000>列表[0]
===> a

为了创建数组,您必须添加作为(type)

  groovy:000> stringarray = ['a','b','c'] as String [] 
===> [a,b,c]
groovy:000> stringarray.class
===>类[Ljava.lang.String;
groovy:000> stringarray.class.array
===> true
groovy:000> stringarray<< 'd'
错误groovy.lang.MissingMethodException:
方法没有签名:[Ljava.lang.String; .leftShift()适用于
参数类型:(java.lang.String )values:[d]
groovy:000> stringarray [0]
===> a

有几个问题, ArrayList Vs LinkedList 何时使用LinkedList<>通过ArrayList<>?,其中涵盖了LinkedList和ArrayList之间的区别。


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 , this and this in java and trying to understand the points mentioned there.

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 datastructure 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.

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 lists over arrays. Groovy does try to smooth out the differences, for instance you can use the size method on arrays to get the number of elements even though in Java you would have to use the length property.

(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 confusion resulting.)

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). Lists are not covariant, due to type erasure. Generic collections rely on the compiler 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: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:

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<>?, which cover the differences between LinkedList and ArrayList.

这篇关于Groovy - List,ArrayList和Object Array之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆