How do I convert a Groovy String collection to a Java String Array?

43,200

To be correct, def data = ["a","b","c"] it is a List, not an array.

Just try casting like this:

def data = ["a","b","c"] as String[]
Share:
43,200
Kevin Williams
Author by

Kevin Williams

Java, Groovy and Android enthusiast who spends his days designing systems for Amazon. By the way, I'm looking to hire 3 senior Java Developers. http://ca-amazon.icims.com/jobs/146841/job;jsessionid=33D0E6136967823BC5E0C5850EB46D79

Updated on December 18, 2020

Comments

  • Kevin Williams
    Kevin Williams over 3 years

    I'm trying to call a methond on a Java class from a Groovy class. The Java method has a String array as a parameter, and I have a collection of Strings in my Groovy class. How do I convert the Groovy collection to a Java String array?

    Java Method:

    public class SomeJavaClass{
      public void helpDoSomething(String[] stuff){
    
      }
    }
    

    Groovy code

    class SomeGroovyClass {
      def data = ["a", "b", "c"]
    
      def doSomething = {
        def javaClass = new SomeJavaClass()
        javaClass(data) //Groovy passes ArrayList, Java class expects String[] ???
      }
    }
    
  • Kevin Williams
    Kevin Williams about 15 years
    I knew there was something groovier than (String[])data.toArray(new String[data.size]) Thanks!!!