URL: http://hansamann.podspot.de/files/grails_podcast_episode_36.mp3
This snippet gives you some interesting examples what you can do with Groovy Lists. Listen to the audio to get the best learning experience. Simply right-click on the title above to download the mp3 file of this part of the series.
The Groovy Series is part of the Grails Podcast and can be subscribed to via: http://hansamann.podspot.de/rss. The series is produced by Dierk König and Sven Haiges, further information about the topic of this episode can be found in the book
Groovy in Action - http://groovy.canoo.com/gina
//Lists //new empty list //creating a populated list with different objects //negative indexes for accessing the final elements myList = [1,2,3,4,5] //adding sth to it myList << "AnotherElement" //but you can also use the java style myList.add("something") //pop an element //cutting and replacing a part of a list list[0..2] = [6,5] // different sizes !! //creating a new list and reversing it //sorting lists myList = [10,5,15,25,20] //summing up myList = [10, 20] //convenience: instead of doing reverse().each{ } you can use reverseEach{} //you can subtract Lists //you can add lists //why is plus() not part of the GDK description??? http://groovy.codehaus.org/groovy-jdk.html#cls44 //you can multiply lists // you cannot divide lists :-) //[1,2,3] / [1,2,3] //won't work //of course you can have lists within lists listOne << listTwo //flatten will remove the inner lists //Windows only //as a special bonus, you can execute the elements of a list. First element is the command, others are parameters //you can do the same with strings by the way ( "cmd /c dir".execute().text ) //def process = ['cmd', '/c', 'dir'].execute() //println process.text //does print the execution of the dir command
Comments
Subscribe to comments
- Posted By: hansamann on February 28, 2007
You need to login to post a comment.
