Search code examples
groovygroovy-console

How we can create List of GString in groovy


ArrayList<GStringImpl> a= ["gaurav " , "ashish"];

println a;

here it gives an error when we used GStringImpl.


Solution

  • The code below works in groovy 2.4.3:

    import org.codehaus.groovy.runtime.GStringImpl
    
    ArrayList<GStringImpl> a = ["gaurav " , "ashish"]
    println a
    println ''
    
    def b = "hello"
    println b.class.name
    println b
    println ''
    
    def c = "worl${100 as char}"
    println c.class.name
    println c
    

    yielding

    [gaurav , ashish]
    
    java.lang.String
    hello
    
    org.codehaus.groovy.runtime.GStringImpl
    world
    

    Your code should work with import org.codehaus.groovy.runtime.GStringImpl.