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