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}) } } |
11 Lists的更多相关文章
- java collections读书笔记(11) Lists
继续这个系列,好久没学习了,懒惰呀. Set接口,实际上是collection 类别中最简单的一个接口,因为它并没有比Collection 接口增加任何的内容,相对而言,大家可能更喜欢List接口和它 ...
- Redis数据类型Strings、Lists常用操作指令
Redis数据类型Strings.Lists常用操作指令 Strings常用操作指令 GET.SET相关操作 # GET 获取键值对 127.0.0.1:6379> get name (nil) ...
- Python命令 (if __name__=="__main__":)
1. 语法 1.以#号开头的语句是注释 2.请务必注意,Python程序是大小写敏感的,如果写错了大小写,程序会报错. 3.按照约定俗成的管理,应该始终坚持使用4个空格的缩进. 4.当语句以冒号:结尾 ...
- 【IOS笔记】View Programming Guide for iOS -1
原文:View Programming Guide for iOS View and Window Architecture Views and windows present your applic ...
- UNIX标准及实现
UNIX标准及实现 引言 在UNIX编程环境和C程序设计语言的标准化方面已经做了很多工作.虽然UNIX应用程序在不同的UNIX操作系统版本之间进行移植相当容易,但是20世纪80年代UNIX版本 ...
- crontab 例子
一个简单的 crontab 示例 0,20,40 22-23 * 7 fri-sat /home/ian/mycrontest.sh 在这个示例中,我们的命令在 7 月的每个星期五和星期六晚上 10 ...
- Client Dataset Basics
文章出处: http://www.informit.com/articles/article.aspx?p=24094 In the preceding two chapters, I discus ...
- Oracle 11gR2 rac 的各项服务说明
安装结束后,会产生一些后台进程来确保集群正常工作并能够与外部通讯.其中的一些有序linux平台的要求需要以root用户权限来启动.比如,网络配置的改动就需要更高的权限.其他后台进程将以grid软 ...
- Laravel之备忘项(不定期更新)
1.自定义字段验证错误信息 $this->validate($request, ['name' => 'required|max:50'], ['name.required' => ...
随机推荐
- 洛谷P2896 [USACO08FEB]一起吃饭Eating Together
题目描述 The cows are so very silly about their dinner partners. They have organized themselves into thr ...
- js代码--根据经纬度计算距离
原网页地址:http://www.storyday.com/wp-content/uploads/2008/09/latlung_dis.html <!DOCTYPE html> < ...
- CentOS6.5中的vsftpd安装配置
安装ftp 1.使用chkconfig 来查看是否装有vsftpd服务: 2.使用yum命令直接安装:yum -y install vsftpd 3.然后为它创建日志文件:touch /var/log ...
- HDOJ1181(简单DFS)(练习使用STL)
#include<iostream> #include<cstdio> #include<string> #include<map> #include& ...
- keepalived+redis 高可用redis主从解决方案
背景介绍: 目前,Redis还没有一个类似于MySQL Proxy或Oracle RAC的官方HA方案.#Redis 2.8版开始正式提供名为Sentinel的主从切换方案(后面附上,未测试) 因此, ...
- 杂项:zabbix(WEB界面的提供分布式系统监视以及网络监视功能)
ylbtech-杂项:zabbix(WEB界面的提供分布式系统监视以及网络监视功能) zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.z ...
- C/C++常用数学函数
math.h/cmath(C++)数学函数库 1 三角函数 double sin (double); double cos (double); double tan (double) ...
- Luogu 3312 [SDOI2014]数表
在这一篇里把所有的套路写全方便自己之后复习. 首先是一个小学生数学:$a$整除$b$ $ = $ $\frac{b}{a}$ 也就是说这题中格子$(i, j)$的值就是既能被$i$整除又能被$j$整 ...
- 教育网bt站点
北京交通大学 晨光BT (http://cgbt.cn)清华晨光BT(http://thubt.cn)北京科技大学 iBeiKeBT(http://bt.ibeike.com)上海大学 乐乎BT (h ...
- CSS学习系列2 -- CSS中的清除浮动
CSS中有一个很常见的问题,就是元素的浮动. 那么,到底什么是元素的浮动呢,我们来看一个例子 举个例子,在一个div里面内部有浮动元素的话,这个浮动元素会让这个div的高度塌陷. .myDiv{ ba ...