Groovy Learning (ii)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.