我正在尝试从Groovy类调用Java类的方法。 Java方法有一个String数组作为参数,我的Groovy类中有一个字符串集合。如何将Groovy集合转换为Java String数组?
Java方法:
public class SomeJavaClass{
public void helpDoSomething(String[] stuff){
}
}
Groovy代码
class SomeGroovyClass {
def data = ["a", "b", "c"]
def doSomething = {
def javaClass = new SomeJavaClass()
javaClass(data) //Groovy passes ArrayList, Java class expects String[] ???
}
}
答案 0 :(得分:88)
要正确,def data = ["a","b","c"]
它是一个List,而不是一个数组。
尝试像这样投射:
def data = ["a","b","c"] as String[]