11 Lists

1       Lists

1.1  定义并访问Lists

List list = new List[].也可以使用泛型。访问list中的元素,可以使用list.get(i) or list[i]。

package list

 

class ListMapTest {

    public static void main(args){

       List<Integer> list = [1,2,3,4];

       println list[2]

       List<Person> persons = list[];

       Person p = new Person("Jim","Knopf")

       persons[0] = p

       println persons.size()

       println persons[0].firstName

       println persons.get(0).firstName

      

    }

}

输出:

Groovy也允许直接访问list中的元素。如下:

package list

 

class ListMapTest2 {

 

    static main(args) {

       List<Person> persons = new ArrayList<Person>();

       persons[0] = new Person("Jim","Knopf")

       persons[1] = new Person("Test","Test")

       println persons.firstName

    }

 

}

输出:

1.2  list与array互转

Groovy自动转换一个Array到一个List,反之亦然。如下:

package list

 

class List2array {

 

    static main(args) {

       def String[] strings = "This is a long sentence".split();

       //转换Array为List

       def List listStrings = strings;

       //转换List为Array

       def String[] arrayStrings = listStrings

       println strings.class.name

       println listStrings.class.name

       println arrayStrings.class.name

    }

 

}

输出:

1.3  List 方法

下边的list方法,非常有用

  • reverse()
  • sort()
  • remove(index)
  • findAll{closure} - returns all list elements for which the closure validates to true
  • first()
  • last()
  • max()
  • min()
  • join("string") 合并list中所有的元素,调用toString方法,并且连接到一起
  • << e 追加元素e到该list

grep 方法,用于过滤集合中指定的元素。

1.4  Operator overloading in Lists

List支持操作符重载。可以使用+来连接字符串,使用-来截取lists并且使用left-shift操作符来向list中增加元素。

1.5  Spreaddot 操作符

*. 分隔符,常被用来调用一个集合中的所有元素。操作的结果是另外一个集合对象。

package list

 

class SpreaddotTest {

 

    static main(args) {

       def list = ["Hello","Test","Lars"]

       //计算list中的每个字符串元素的长度

       def sizeList = list*.size()

       assert sizeList == [5,4,4]

    }

 

}

输出:

空,说明正确。

1.6  搜索list(find, findall and grep)

搜索方法:

  • findAll{closure} - returns all list elements for which the closure validates to true
  • find{closure} - returns the list element for which the closure validates to true
  • grep(Object filter) - Iterates over the collection of items and returns each item that matches the given filter - calling the Object#isCase. This method can be used with different kinds of filters like regular expressions, classes, ranges etc.

package list

 

class FindAndGrepTest {

 

    static main(args) {

       def l1 = ['test',12,20,true]

       //检索Boolean类型的元素

       assert[true] == l1.grep(Boolean)

       //检索以G开头的元素

       assert['Groovy'] == ['test','Groovy','Java'].grep(~/^G.*/)

       //返回list中包含b和c的元素,注:['b', 'c'],是一个集合

       assert ['b', 'c'] == ['a', 'b', 'c', 'd'].grep(['b', 'c'])

       //返回在range内的元素

       assert[14,16]==[5,14,16,75,12].grep(13..17)

       //equal

       assert[42.031] == [15,'Peter',42.031,42.032].grep(42.031)

       //返回基于闭包的大于40的数

       assert[50,100,300] == [10, 12, 30, 50, 100, 300].grep({it > 40})

    }

 

}

原文地址:https://www.cnblogs.com/yaoyuan2/p/5708970.html