Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of strings in groovy

In ruby, there is a indiom to create a array of strings like this:

names = %w( lucas Fred Mary ) 

Is there something like that in groovy?

like image 468
Lucas Avatar asked Feb 18 '10 16:02

Lucas


People also ask

Does Groovy have array?

Groovy reuses the list notation for arrays, but to make such literals arrays, you need to explicitly define the type of the array through coercion or type declaration. You can also create multi-dimensional arrays.

How do I read an array in Groovy?

In Groovy, you can access array item by using square bracket with an index ( [index] ) or using getAt(index) function.

What does [] mean in Groovy?

[:] creates an empty Map. The colon is there to distinguish it from [] , which creates an empty List. This groovy code: def foo = [:] is roughly equivalent to this java code: Object foo = new java.util.LinkedHashMap();

How do you define a string in Groovy?

A String literal is constructed in Groovy by enclosing the string text in quotations. Groovy offers a variety of ways to denote a String literal. Strings in Groovy can be enclosed in single quotes ('), double quotes (“), or triple quotes (“””). Further, a Groovy String enclosed by triple quotes may span multiple lines.


1 Answers

If you really want to create an array rather than a list use either

String[] names = ["lucas", "Fred", "Mary"] 

or

def names = ["lucas", "Fred", "Mary"].toArray() 
like image 57
Dónal Avatar answered Sep 17 '22 13:09

Dónal