3, collection
Groovy supports collection, List, MAP, and arrays
(1) Lists
l The following is an example of creating a list, [] indicates an empty List expression
List = [5, 6, 7, 8]
Assert List.get (2) == 7
Assert List instanceof java.util.list
EmptyList = []
askERT EMPTYLIST.SIZE () == 0
EmptyList.Add (5)
askERT EMPTYLIST.SIZE () == 1
l Each List expression is the implementation of java.util.list.
(2) Ranges
l Range Allows to create a list of continuous values
l Since Range extension java.list, Range can be used as a list
l Use ..'s Range is included in two boundaries, using the Range only includes the start boundary without including the end boundary.
// an Inclusive Range
Range = 5..8
askERT RANGE.SIZE () == 4
askERT RANGE.GET (2) == 7
askERT RANGE InstanceOf Java.util.List
askERT RANGE.CONTAINS (5)
askERT RANGE.CONTAINS (8)
// lets use an exclusive r
Range = 5 ... 8
askERT RANGE.SIZE () == 3
askERT RANGE.GET (2) == 7
askERT RANGE InstanceOf Java.util.List
askERT RANGE.CONTAINS (5)
Assert! Range.Contains (8)
l Range can be used to implement java objects of java.lang.comparable
// an Inclusive Range
Range = 'a' .. 'd'
askERT RANGE.SIZE () == 4
askERT RANGE.GET (2) == 'c'
askERT RANGE InstanceOf Java.util.List
Assert Range.Contains ('a')
Assert Range.Contains ('D')
Assert! Range.Contains ('e')
l Range can be used for loop traversal
For (i in 1..10) {
Println "Hello $ {i}"
}
(3) MAPS
l The following is an example of creating MAP, [:] indicates an empty MAP expression
Map = ["Name": "Gromit", "Likes": "Cheese", "ID": 1234]
ask.GET ("Name") == "Gromit"
ask.get ("ID") == 1234
Assert map instanceof java.util.map
Emptymap = [:]
askERT EMPTYMAP.SIZE () == 0
EmptyMap.Put (5, "foo") assert emptyMap.size () == 1
Assert EmptyMap.Get (5) == "foo"
l Map can operate like Beans, but the Key value (similar attribute name) must be a valid String ID
Map = ["Name": "Gromit", "Likes": "Cheese", "ID": 1234]
ask.NAME == "Gromit"
ask.ID == 1234
Emptymap = [:]
askERT EMPTYMAP.SIZE () == 0
EMPTYMAP.FOO = 5
askERT EMPTYMAP.SIZE () == 1
askERT EMPTYMAP.FOO == 5
(4) Use the subscript operator
l You can use the subscript in the string, lists, maps ...
Text = "Nice Cheese Gromit!"
X = text [2]
askERT X == "C"
askERT X.CLASS == String
Sub = text [5..10]
askERT SUB == 'CHeese'
Map = ["Name": "Gromit", "Likes": "Cheese", "ID": 1234]
Assert Map ['Name'] == "Gromit"
List = [10, 11, 12]
Answer = List [2]
askASERT ANSWER == 12
List = 1..200
Sub = List [1, 3, 20..25, 33]
Assert Sub == [101, 103, 120, 121, 122, 123, 124, 125, 133]
l You can use the subscript operator update item
List = ["a", "b", "c"]
List [2] = "d"
List [0] = list [1]
List [3] = 5
Assert List == ["B", "B", "D", 5]
l You can use a negative index from the last start count
Text = "Nice Cheese Gromit!"
X = text [-1]
askERT X == "!"
Name = text [-7 ..- 2]
askERT NAME == "Gromit"
L can also use the rearward range (starting an index greater than the end index), the result is reversed
Text = "Nice Cheese Gromit!"
Name = text [3..1]
askERT NAME == "ECI"