May 14, 2021 Groovy
A list is a structure that stores a collection of data items. In Groovy, List saves a series of object references.
Object references in List occupy a position in the sequence and are distinguished by integer indexes.
List text is represented as a series of objects separated by commas and enclosed in square brackets.
The groovy list is indexed using the index operator. The list index starts at 0 and refers to the first element.
The data in a list in groovy can be of any type. The list of collections under java is somewhat different, and the list under java is a collection of data of the same type.
Groovy lists can be nested. F or example, the "aaa" groovy list has a built-in inverse method reverse(). C alling List.reverse can reverse the list. T he groovy list has a sort method built into it. C alling List.sort() enables list sorting. An empty list is represented as an empty collection declared by :
def list1 = []
def list2 = [1,2,3,4]
list2.add(12)
list2.add(12)
println list1.size()
Several common methods between lists:
1, add:
def list1 = [100, 101]
def list2 = [ 99,98,1]
println list2.plus(list1) //输出结果: [100, 101, 99, 98,1]
// list2.plus(list1) 也可以写成 list2 + list1
2, delete:
def list1 = [12, 13]
def list2 = [11, 2, 33, 12, 13, 16]
println list2.minus(list1) //输出结果: [11, 2, 33, 16]
//list2.minus(list1) 也可以写成 list2 - list1
In this chapter, we'll discuss the list methods available in Groovy.
Serial number | Method and description |
---|---|
1 |
add()
Attach the new value to the end of this list. |
2 |
contains()
True is returned if this list contains the specified value. |
3 |
get()
Returns the elements that specify the location in this list. |
4 |
isEmpty()
True is returned if this list does not contain elements |
5 |
minus()
Create a new list of original elements instead of the elements specified in the collection. |
6 |
plus()
Create a new list of the original elements and the elements specified in the collection. |
7 |
pop()
Remove the last item from this list |
8 |
remove()
Remove the elements that specify a location in this list. |
9 |
reverse()
Create a new list that is opposite to the elements of the original list |
10 |
size()
Gets the number of elements in this list. |
11 |
sort()
Returns a sorted copy of the original list. |