Groovy预览--lists


1 lists

  java通过使用方括号和下标索引数组,groovy使用了同样的方法来支持list-java.util.List的实例,java.util.List允许向列表中增加或者删除对象,允许在运行时改变列表的大小,保存在列表中的对象不受类型的限制。另外,在groovy中可以通过超出列表范围的数来索引列表,再一次表明可以改变列表的大小,此外,列表也可以在代码中指定

  

def roman=['','I','II','III','IV','V','VI','VII']
println roman.size()
println roman[4]
println roman[9]
roman[9]='VIII'
println roman[9]

results:
8
IV
null
VIII
通过roman.size()可以获取list的大小为8,通过下标可以得到相应位置的元素值。
注意:println roman[9]并没有抛出异常,而是输出null。roman[9]='VIII'赋值时,该位置其实并不存在,代码中操作了超出列表范围的位置

list操作符
GDK使用range和collection作为参数重载了getAt方法,这样可以使用一个范围或者索引的一个集合来访问list
longlist=(1..10).toList();
print longlist // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

dplist=new ArrayList();
dplist.addAll(longlist)//list复制
println(dplist)// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

println(dplist[0..4])//使用range访问,输出: [1, 2, 3, 4, 5]

dplist[0..2]=['x','y','z']//使用range赋值
println(dplist)// [x, y, z, 4, 5, 6, 7, 8, 9, 10]
 

list也可以通过使用负数进行索引,通过负数索引是从列表的最后往前走,list[-1]获取最后一个元素,list[-2]获取倒数第二个元素,也可以使用负数的范围list[-3..-1]。当使用反向的范围访问时,得到的结果也是反向的

list=(1..10).toList()
println list  //[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

println list[-3..-1] //负数范围 [8, 9, 10]
println list[-1..-3]//范围反向 [10, 9, 8]
虽然下标操作符能用来改变一个列表中的任意单个元素,也有一些操作符用来改变list的内容,它们是plus(Object),plus(Collection),leftShift(Object),minus(Collection)和multiply
mylist=[]
println mylist // []

mylist+='a'
println mylist // [a]

mylist+=['b','c']
println mylist // [a, b, c]

mylist << 'd' <<'e'
println mylist // [a, b, c, d, e]


mylist-='e'
println mylist // [a, b, c, d]

mylist-=['b','d']
println mylist // [a, c]

println mylist*2 //[a, c, a, c]

 

下面展示了向列表增加元素、从列表删除元素,通过不同的途径组合列表,排序,翻转列表和平整嵌套的列表,从存在的列表创建新的列表

 

list1=[1,[2,3],[4,5]]
println list1                   //[1, [2, 3], [4, 5]]
println list1.flatten()         //[1, 2, 3, 4, 5] 平整嵌套的列表 

list2=(1..5).toList()           //[1, 2, 3, 4, 5]
list3=list1.intersect(list2)    //取出交集
println list3                   //[1]

println list3.disjoint(list1)   //false 判断是否有交集

list=[1,2,3]
popped=list.pop()               //像堆栈一样操作
println list                    //[1, 2]
println popped                  //3

//排序
listChar=['a','d','e','b','f','c']    //['a','d','e','b','f','c']
listChar=listChar.sort{a,b-> a <=> b} // [a, b, c, d, e, f]

//删除元素

listChar.remove(2)              //根据索引删除
println listChar                //[a, b, d, e, f]
listChar.remove('b')            //删除指定元素
println listChar                //[a, d, e, f]

listChar=['a','d','e','d','e']  //['a','d','e','d','e']
listChar.remove('d')            //只删除第一个匹配的元素
println listChar                //[a, e, d, e]
listChar.removeAll('e')         //删除所有的e
println listChar                //[a, d]

listChar=['a','d','e','b','f','c']    //['a','d','e','b','f','c']
def upper=listChar.collect{it->it.toUpperCase()}
println upper                    //[A, D, E, B, F, C]     

 List有许多方法用来访问list中元素,遍历列表和接收结果。
查询方法包括查询list中元素数量的count方法,min和max方法用来查询list中的最小元素和最大元素,find方法用来查找list第一个符合闭包要求的元素,every和any

方法用来确定list中的每一个元素(或者任何一个元素)是否符合闭包的要求。 遍历像通常一样完美,使用each方法进行正向遍历,并且使用reverseEach方法进行反向遍历。 这些方法是简单优雅的,join方法十分简单:这个方法将所有元素通过给定的字符串进行连接,然后将结果作为字符串返回,inject方法来自Smalltalk,这个方法使用闭包注入一个新的函数,这个函数用来对一个中间结果和遍历的当前元素进行操作,inject方法的第一个参数是中间结果的初始值,使用这个方法累加所有元素,后面对所有的元素结果进行相乘。

def list=(1..10).toList()
println list    //[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def even=list.findAll{it -> it%2 == 0 }
println even    //[2, 4, 6, 8, 10]
println list.every{it -> it < 5 }    //false
println list.any{it -> it < 5 }      //true

def store=''
list.each{it -> store += it }
println store    //12345678910
store=''
list.reverseEach{it -> store += it }
println store    //10987654321

println list.join('-')    //1-2-3-4-5-6-7-8-9-10


result=list.inject(0){clinks,guests -> clinks+=guests}
println result    //55

result=list.inject(1){fac,guests -> fac*=guests}
println result    //3628800

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM