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})

}

}

11 Lists的更多相关文章

  1. java collections读书笔记(11) Lists

    继续这个系列,好久没学习了,懒惰呀. Set接口,实际上是collection 类别中最简单的一个接口,因为它并没有比Collection 接口增加任何的内容,相对而言,大家可能更喜欢List接口和它 ...

  2. Redis数据类型Strings、Lists常用操作指令

    Redis数据类型Strings.Lists常用操作指令 Strings常用操作指令 GET.SET相关操作 # GET 获取键值对 127.0.0.1:6379> get name (nil) ...

  3. Python命令 (if __name__=="__main__":)

    1. 语法 1.以#号开头的语句是注释 2.请务必注意,Python程序是大小写敏感的,如果写错了大小写,程序会报错. 3.按照约定俗成的管理,应该始终坚持使用4个空格的缩进. 4.当语句以冒号:结尾 ...

  4. 【IOS笔记】View Programming Guide for iOS -1

    原文:View Programming Guide for iOS View and Window Architecture Views and windows present your applic ...

  5. UNIX标准及实现

    UNIX标准及实现 引言     在UNIX编程环境和C程序设计语言的标准化方面已经做了很多工作.虽然UNIX应用程序在不同的UNIX操作系统版本之间进行移植相当容易,但是20世纪80年代UNIX版本 ...

  6. crontab 例子

    一个简单的 crontab 示例 0,20,40 22-23 * 7 fri-sat /home/ian/mycrontest.sh 在这个示例中,我们的命令在 7 月的每个星期五和星期六晚上 10 ...

  7. Client Dataset Basics

    文章出处:  http://www.informit.com/articles/article.aspx?p=24094 In the preceding two chapters, I discus ...

  8. Oracle 11gR2 rac 的各项服务说明

       安装结束后,会产生一些后台进程来确保集群正常工作并能够与外部通讯.其中的一些有序linux平台的要求需要以root用户权限来启动.比如,网络配置的改动就需要更高的权限.其他后台进程将以grid软 ...

  9. Laravel之备忘项(不定期更新)

    1.自定义字段验证错误信息 $this->validate($request, ['name' => 'required|max:50'], ['name.required' => ...

随机推荐

  1. PRVF-0002 : could not retrieve local node name

    安装 oracle 的时候,./runInstaller 启动报错  PRVF-0002 : could not retrieve local node name 碰到这个错误是因为 OUT试图对你主 ...

  2. js之__proto__原型链

    可参考: http://blog.csdn.net/irelandken/article/details/7297490

  3. 【转】Pro Android学习笔记(三六):Fragment(1):基本概念

    目录(?)[-] 为何引入Fragment 大小屏幕的适配 横屏竖屏切换 返回键 什么是Fragment 为何引入Fragment 我们之前的Activity都是都是全屏处理较为简单的单一事务功能,适 ...

  4. java代码GUI简单的。。。

    总结:觉得 package com.da; import java.awt.*; //逆向思维:important //创建一个String对象的数组,然后执行读取文本,把文本每一行存入数组,它将读取 ...

  5. bzoj2118

    最短路 很早以前做的了 数据范围太大,不能直接算 mn=min(a[i]) 算出d[i]表示sum%mn=i最小能构成的数,这个用最短路就行了,然后计算d[i],d[i]+mn的个数统计答案 #inc ...

  6. Neural Networks and Deep Learning 笔记

    1 Introduction to Deep Learning 介绍了神经网络的定义,有监督学习,分析了为什么深度学习会崛起 1.1 结构化数据/非结构化数据 结构化数据:有一个确切的数据库,有key ...

  7. Centos 查看机器型号

    测试机器的硬件信息: 查看CPU信息(型号) # cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c 8 Intel(R) Xeon(R) CP ...

  8. 在命令行上启动genymotion虚拟机

    自从有了genymotion,多机联调就解放了,一台电脑运行两个genymotion虚拟机毫无压力,不过也看用的是哪种os image,之前我以为google自己的Nexus应该最适应,哪知道开起来比 ...

  9. SRAtoolkit软件的使用介绍

    Using the SRA Toolkit to convert .sra files into other formats Sequence Read Archive Submissions Sta ...

  10. netty的引用计数

    netty的引用计数文档看http://netty.io/wiki/reference-counted-objects.html 为什么会引用引用计数呢,Java中不是有gc线程帮我们回收对象吗?我个 ...