mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-01-14 13:15:59 +00:00
Added a few more examples for List manipulation
This commit is contained in:
parent
39b96dd2db
commit
68f26804c5
@ -54,15 +54,26 @@ println x
|
|||||||
//Creating an empty list
|
//Creating an empty list
|
||||||
def technologies = []
|
def technologies = []
|
||||||
|
|
||||||
//Add an element to the list
|
/*** Adding a elements to the list ***/
|
||||||
technologies << "Groovy"
|
|
||||||
|
// As with Java
|
||||||
technologies.add("Grails")
|
technologies.add("Grails")
|
||||||
|
|
||||||
|
// Left shift adds, and returns the list
|
||||||
|
technologies << "Groovy"
|
||||||
|
|
||||||
|
// Add multiple elements
|
||||||
technologies.addAll(["Gradle","Griffon"])
|
technologies.addAll(["Gradle","Griffon"])
|
||||||
|
|
||||||
//Remove an element from the list
|
/*** Removing elements from the list ***/
|
||||||
|
|
||||||
|
// As with Java
|
||||||
technologies.remove("Griffon")
|
technologies.remove("Griffon")
|
||||||
|
|
||||||
//Iterate over elements of a list
|
// Subtraction works also
|
||||||
|
technologies = technologies - 'Grails'
|
||||||
|
|
||||||
|
// Iterate over elements of a list
|
||||||
technologies.each { println "Technology: $it"}
|
technologies.each { println "Technology: $it"}
|
||||||
technologies.eachWithIndex { it, i -> println "$i: $it"}
|
technologies.eachWithIndex { it, i -> println "$i: $it"}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user