Array of strings in groovy

ArraysStringGroovy

Arrays Problem Overview


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?

Arrays Solutions


Solution 1 - Arrays

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

Solution 2 - Arrays

Most of the time you would create a list in groovy rather than an array. You could do it like this:

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

Alternately, if you did not want to quote everything like you did in the ruby example, you could do this:

names = "lucas Fred Mary".split()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLucasView Question on Stackoverflow
Solution 1 - ArraysDónalView Answer on Stackoverflow
Solution 2 - ArraysChris DailView Answer on Stackoverflow