Groovy中的列表和Java的数组类似,如:数组
def numbers = [1, 2, 3, 4]spa
Groovy列表使用索引操做符[]来表示元素值,[]是List类的getAt方法的重载,用法和字符索引获取子字符串类似,同时列表之间能够进行+、-、赋值、<<等操做。orm
映射就是你们熟悉的键值对的无序集合,key值不能为变量,例如:索引
def x = 1字符串
def y = 2get
def m = [x : y, y : x] //m = ['x' : 2, 'y' : 1]it
映射也是经过getAt方法获取索引的值的,一样也是使用putAt方法进行赋值:console
def names = ['Rex' : 'Barclay', 'David' : 'Savage']import
def divisors = [4 : [2], 6 : [3, 2], 12 : [6, 3, 2, 1]]变量
names['Rex'] //'Barclay' names.getAt('Rex')
names.Rex //'Barclay'
names['John'] //null
divisors[6] //[3, 2]
divisors[6] = [1] //[4 : [2], 6 : [1], 12 : [6, 3, 2, 1]]
从上面能够看出映射的键能够是String类型的,也能够是Integer类型的,同时这两种类型能够混用:
def names = [1 : 'Rex', '1' : 'David']
names[1] //'Rex'
names['1'] //'David'
范围是表达特定序列值的一种简略方法,有时能够当特定序列的数组用。
1..10 // 1, 2, 3, 4, ...., 10
10..1 // 10, 9, 8, ...., 1
1..<10 // 1, 2, 3, 4, ...., 9
'A'..'D' // A, B, C, D
def start = 10
def finish = 12
start..finish + 1 //[10, 11, 12, 13]
print "hello word"
println "hello word"
printf 'hello %s', 'word'
支持以上三种输出方式,print、println差异在print打印文本后不换行,均可以加上括号使用。printf用法和不少语言一致。
System.in.readLine() //获取输入的一行字符串,以换行结束,若是但愿获得其余类型的数据,只须要以后toInteger(),......
Console.readString() //须要import console.*, 获取输入的字符串能够以空格结束
Console.readInteger()//获取Integer数据