mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-10 08:44:28 +02:00
Added a few more examples for List manipulation
This commit is contained in:
@@ -54,14 +54,25 @@ 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")
|
||||||
|
|
||||||
|
// Subtraction works also
|
||||||
|
technologies = technologies - 'Grails'
|
||||||
|
|
||||||
// Iterate over elements of a list
|
// 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"}
|
||||||
|
Reference in New Issue
Block a user