class Person { String address Name name } class Name { String first String last } List ps = [] // create mockup data 10.times { i -> Name nm = new Name(first:"first ${i + 1}", last:"last $i") ps << new Person(name:nm, address:"${(i+1)*7} Street") } assert ps.class.name == "java.util.ArrayList" //print out each array element for (p in ps) { System.out.print(p.address + ", ") }; System.out.println("") // Java, yuck! ps.each { p -> print "${p.address}, " }; println "" // Groovy println ps.collect { p -> p.address } // returns list, more Groovy // Now for some GPath magic!! println ps.address // returns list, same as .collect above assert ps.name.class.name == "java.util.ArrayList" // list of Names assert ps.name[0].class.name == "Name" println ps.name.first // returns list println ps.name.last // returns list
http://groovyconsole.appspot.com/script/449005
No comments:
Post a Comment