[英]Set array to variable in groovy
New to groovy and not a java lover. Groovy的新手,不是Java爱好者。 In my jenkinsfile , I'm having an issue with doing what I think would be simple.
在我的jenkinsfile中 ,我在做简单的事情时遇到了问题。
SURL = new String[3]
for (int i = 0; i < 3; i++)
{
url="value"
SURL[i]="${url}"
}
Seems like in this simple example that SURL[0] through SURL[2] would be set to "value". 在这个简单的示例中,似乎SURL [0]到SURL [2]将被设置为“值”。 I'm getting the error:
我收到错误消息:
java.lang.ArrayStoreException: org.codehaus.groovy.runtime.GStringImpl
Any help is appreciated. 任何帮助表示赞赏。 Thx!
谢谢!
This seems like a pretty contrived example, I'm not sure what you're really trying to do. 这似乎是一个非常人为的例子,我不确定您到底要做什么。
If url
is already a String
why not add it directly to SURL
? 如果
url
已经是String
为什么不将其直接添加到SURL
? Putting it in "${}"
gives you a GString
instead. 将其放在
"${}"
会给您一个GString
。
It's not very Groovy to use a statically typed String array, just use a list. 使用静态类型的String数组不是很Groovy,仅使用列表即可。
def SURL = []
3.times {
SURL << url
}
This example uses the overloaded <<
operator to append to the list. 本示例使用重载的
<<
运算符将其追加到列表中。
If you want to make it right, consider to define the array type explicitly. 如果要正确处理,请考虑显式定义数组类型。 Instead of
代替
def SURL = new String[3]
SURL[ 0 ] = "-- $a" // << here comes ArrayStoreException: org.codehaus.groovy.runtime.GStringImpl
do 做
String[] SURL = new String[3]
SURL[ 0 ] = "-- $a"
then it runs smoothly and groovy can properly outbox the GString
value to String
. 然后它会平稳运行,并且常规可以正确地将
GString
值发送到String
。
Ended up setting it as a string like this: 最终将其设置为这样的字符串:
SURL[i]="${url}" as String
Still unsure why it's functioning this way. 仍然不确定为什么它会以这种方式运行。 Maybe thinking it's an object?
也许认为这是一个对象?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.