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

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

问题描述

我想了解 groovy ListArrayListObject Array 之间的区别,但找不到真实(简单)的例子.比如,我们可以用 Array 做什么,而用 ListArrayList 做不到?我知道 Array 是一个固定的对象序列.顺便提一下,我看过 this , thisthis 在 java 中并试图理解那里提到的要点.>

我希望我能清楚地描述我的问题,但如果我不清楚或完全没有抓住重点,请告诉我.有人可以指出我正确的方向吗?谢谢!

解决方案

是的,Array 是一种固定大小的数据结构.它被声明为具有描述它可以容纳哪些元素的类型,该类型是协变的(参见此处了解协变与逆变).Array 在运行时知道它的类型,试图在 Array 中放入任何不合适的东西都会导致异常.

在 Groovy 中,由于低级和不灵活(固定大小),数组并不是真正惯用的.它们支持与 Java 的互操作.通常,使用 Groovy 的人更喜欢 List 而不是 Array.Groovy 确实尝试消除差异,例如您可以在 Array 上使用 size 方法来获取元素的数量(即使在 Java 中您必须使用 length 属性).

(在 Ruby 中,最类似于列表的数据结构称为 Array,因此没有 Java 背景的从 Rails 转到 Groovy 或 Grails 的人往往会沿用命名法,从而导致混淆.)

java.util.List 是一个接口,描述了由不同类型的列表实现的基本列表操作.列表使用泛型类型参数来描述它们可以保存的内容(类型在 Groovy 中是可选的).Lists 上的泛型类型是不变的,而不是协变的.泛型集合依赖编译时检查来强制类型安全.

在 Groovy 中,当您使用文字语法 (def mylist = []) 创建列表时,java.util.ArrayList 是您获得的实现:

groovy:000>列表 = ['a', 'b', 'c']===>[]时髦:000>列表实例列表===>真的时髦:000>列表.class===>类 java.util.ArrayList时髦:000>列表.class.array===>错误的时髦:000>列表<<'d'===>[d]时髦:000>列表[0]===>一种

为了创建一个数组,你必须将 as (type)[] 添加到声明中:

groovy:000>stringarray = ['a', 'b', 'c'] as String[]===>[a, b, c]时髦:000>字符串数组.class===>类 [Ljava.lang.String;时髦:000>字符串数组.class.array===>真的时髦:000>字符串数组<<'d'错误 groovy.lang.MissingMethodException:没有方法签名:[Ljava.lang.String;.leftShift() 是适用的对于参数类型:(java.lang.String) 值:[d]时髦:000>字符串数组[0]===>一种

已经有几个问题了,ArrayList Vs LinkedList何时使用LinkedList<>而不是ArrayList<>?,其中涵盖了LinkedListArrayList 之间的区别.

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

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

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

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.

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天全站免登陆