List:
The list in groovy is similar to an array of Java, such as:
def numbers = [1, 2, 3, 4]
The groovy list uses the index operator [] to represent the element value, [] is the overload of the GetAt method of the list class, the usage and the character index get a substring similar, and the list can be 、-、 assignment, <<, and so on.
Mapping (MAP):
Mapping is an unordered collection of key-value pairs that you are familiar with, and the key value cannot be a variable, for example:
def x = 1
Def y = 2
def m = [X:y, y:x]//m = [' x ': 2, ' Y ': 1]
The mapping is also used to get the value of the index through the GetAt method, and is also assigned using the Putat method:
def names = [' Rex ': ' Barclay ', ' David ': ' Savage ']
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]]
You can see from the above that the keys of a map can be of type string or integer, and both types can be mixed:
def names = [1: ' Rex ', ' 1 ': ' David ']
NAMES[1]//' Rex '
names[' 1 ']//' David '
Scope (range):
A range is a shorthand method for expressing a particular sequence of values, which can sometimes be used as an array of specific sequences.
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]
Basic output:
print "Hello word"
println "Hello word"
printf ' Hello%s ', ' word '
Support the above three kinds of output mode, print, println difference in print after printing text does not wrap, you can use parentheses. printf usage is consistent with many languages.
Basic input:
System.in.readLine ()//Gets the input line string, ends with a newline, and if you want to get other types of data, only after Tointeger (),......
Console.readstring ()//need import console.*, get input string can end with a space
Console.readinteger ()//Get Integer data
Groovy Learning (ii)