Jan 22, 2009

Common Groovy Problem

When I write some Groovy code in my application, I find an error when declare a trivial String Array:
String[] myStrArr = new String[]{"Groovy","is","so sexy!"}
So, how can we use String Array in Groovy?
def myStrList = ["Groovy","is","so sexy!"] // myStrList is of ArrayList type.
def myStrArr = (String[])myStrList // myStrArr is now of String[] type

myStrArr.each {
print "${it} " // Groovy is so sexy!
}
The answer is that Groovy has its strong class - "ArrayList". ArrayList in Groovy is much more convenient and very easy to use.
Convert one ArrayList to a String Array in Groovy is ... just say Wow!
so, println "How sexy Groovy is!"

0 comments:

Post a Comment

Write what you think, what you like!