Groovy Goodness: Removing the Last Item From Lists [Snippet]
2.5.0之前的Groovy版本对
在下面的sampleGroovy代码中,我们使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | def List = ['Groovy', 'is', 'great!'] // Remove last item from List // with removeLast(). assert List.removeLast() == 'great!' assert List == ['Groovy', 'is'] // Remove last item which is now 'is'. List.removeLast() // add new item to end of the List. List.add 'rocks!' assert List.join(' ') == 'Groovy rocks!' /* IMPORTANT */ /* pop() and push() implementations has changed */ /* in Groovy 2.5.0. They now work on the first */ /* item in a List instead of the last. */ // Using pop() we remove the first item // of a List. assert List.pop() == 'Groovy' // And with push we add item to // beginning of a List. List.push 'Spock' assert List.join(' ') == 'Spock rocks!' |
用Groovy 2.5.0编写。