Groovy - Difference between list, ArrayList and array of objects

I tried to understand the difference between groovy List , ArrayList and Object Array but could not find real (simple) examples. For example, what can we do with Array , which cannot be done with List or ArrayList ? I understand that Array is a fixed sequence of objects. Just to mention that I looked at this , this and this in Java, and tried to understand the points mentioned there.

I hope that I describe my problem clearly, but let me know if I am not clear or completely miss the point. Can someone point me in the right direction? Thank you!

+11
source share
3 answers

Yes, Array is a fixed-size data structure. It is declared as having a type that describes which elements it can contain; this type is covariant ( see here covariant or contravariant elements). Array knows its type at runtime, and trying to put something inappropriate in Array will throw an exception.

In Groovy, arrays are not completely idiomatic due to their low level and inflexibility (fixed size). They are supported for interacting with Java. Usually people using Groovy prefer List Array . Groovy is trying to smooth out the differences, for example, you can use the size method in Array to get the number of elements (even if you have to use the length property in Java).

(In Ruby, the data structure closest to the list is called Array , so people coming to Groovy or Grails from Rails without a Java background tend to migrate the item, which leads to confusion.)

java.util.List is an interface that describes the basic operations on lists that are implemented by various types of lists. Lists use generic type parameters to describe what they can contain (types are optional in Groovy). Lists are not covariant due to type erasure. Shared collections rely on the compiler to provide type safety.

In Groovy, when you create a list using literal syntax ( def mylist = [] ), 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 

To create an array, you must add as (type)[] in 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 a few questions, ArrayList vs. LinkedList and when to use LinkedList <> on top of ArrayList <>? that cover the differences between LinkedList and ArrayList .

+14
source

You can find the differences between ArrayList and LinkedList , this is an implementation of List (interface). Each implementation has different methods. You can see these methods in:

* LinkedList methods

* ArrayList methods

The list cannot be compared with ArrayList.

+4
source

List is an interface and ArrayList implementation with certain characteristics. As with all other programming languages, Java also has specific containers for certificate issues. You can get an initial understanding here: http://docs.oracle.com/javase/1.5.0/docs/guide/collections/overview.html

+1
source

Source: https://habr.com/ru/post/982394/


All Articles