//1、Remove def a = [1,2,3,4] def b = [3,4,5] a.add(5) // a = [1,2,3,4,5] a.remove(5) // faile 因为这代表移除list中的第五项,显然越界。 a.remove(4) // a = [1,2,3,4] b.addAll(a) // b = [1,2,3,4,5] b.removeAll[a] // b = [5] b.clear() // b = [] //2、Transpose def a = [1, 2, 3] def b = [4, 5, 6] def ab = [a, b].transpose() // ab = [[1, 4], [2, 5], [3, 6]] def m = [:] [a, b].transpose().each { k, v -> m += [(k): v] } // m == [1: 4, 2: 5, 3: 6] //相当于 (def m = [a, b].transpose().inject([:]) { s, l -> s + [(l[0]): l[1]] }) //注: 组合的数组中不能出现重复(纯数字项)。 def xxx = [1,2,3,4,5,6] def ooo = ['a','b','c','d','e','f'] def ccc = [xxx, ooo].transpose() println ccc [xxx, ooo].transpose().eachWithIndex {value, key -> print "$key $value \n" } // [[1, a], [2, b], [3, c], [4, d], [5, e], [6, f]] // 0 [1, a] // 1 [2, b] // 2 [3, c] // 3 [4, d] // 4 [5, e] // 5 [6, f]