Groovy的列表

1.列表
1
2
3
def list = [1, 3, 1]
println list.class
println list.size()
2.数组
1
2
3
4
def array = [1, 2, 1] as int[]
println array.class
println array.size()
int[] array2 = [1, 2, 4]
1
2
3
4
5
6
//排序
def sortList = [2, 5, 1, 1, -3, 4, 7, 0]
//Comparator mc = { a, b -> a == b ? 0 : Math.abs(a) < Math.abs(b) ? -1 : 1 }
//Collections.sort(sortList,mc)
sortList.sort{a, b -> a == b ? 0 : Math.abs(a) < Math.abs(b) ? -1 : 1 }
//println sortList
1
2
3
4
//查找
def findList = [2, 5, 1, 1, -3, 4, 7, 0]
int res1 = findList.find {it % 2 !=0}
def res2 = findList.findAll {it % 2 == 0}