Thursday, 24 January 2019

Groovy: List: append elements to list


There are many ways to append elements to a list.
a.   Using << operator
b.   Using leftShift method
c.    Using add method
d.   Using addAll method
e.   Using + operator
f.     Using plus method

HelloWorld.groovy
list1 = [2, 3, 5]
println "list1 : $list1"

list1 << 7
println "list1 : $list1"

list1 = list1.leftShift(11)
println "list1 : $list1"

list1.add(13)
println "list1 : $list1"

list1.addAll([17, 19])
println "list1 : $list1"

list1 = list1 + 23
println "list1 : $list1"

list1 = list1.plus(29)
println "list1 : $list1"


Output
list1 : [2, 3, 5]
list1 : [2, 3, 5, 7]
list1 : [2, 3, 5, 7, 11]
list1 : [2, 3, 5, 7, 11, 13]
list1 : [2, 3, 5, 7, 11, 13, 17, 19]
list1 : [2, 3, 5, 7, 11, 13, 17, 19, 23]
list1 : [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]



Previous                                                 Next                                                 Home

No comments:

Post a Comment