string groovy 中的字符串数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2290002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:40:40  来源:igfitidea点击:

Array of strings in groovy

arraysstringgroovy

提问by Lucas

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

在 ruby​​ 中,有一个成语可以创建这样的字符串数组:

names = %w( lucas Fred Mary )

Is there something like that in groovy?

groovy 中有类似的东西吗?

采纳答案by Chris Dail

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

大多数情况下,您会在 groovy 中创建列表而不是数组。你可以这样做:

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

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

或者,如果您不想像在 ruby​​ 示例中那样引用所有内容,您可以这样做:

names = "lucas Fred Mary".split()

回答by Dónal

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