加载中...

Groovy基础语法-list

定义list

groovy:000> ls1 = [1,2,3,4,5,6,7]
===> [1, 2, 3, 4, 5, 6, 7]

list基本操作

1、将元素添加到list尾部

groovy:000> ls1.add(8)
===> true

2、移除List中的元素
根据下标删除元素

groovy:000> ls1.remove(1)
===> 2  # 将list中2给移除了,下标为1

3、修改list中的元素

# 方式1
groovy:000> ls1.set(0,10) #将索引为0的元素修改为10
===> 1
groovy:000> ls1
===> [10, 3, 4, 5, 6, 7, 8]

# 方式二
# 将List中索引为0的元素更改为10
groovy:000> ls1.putAt(0,10) 

4、获取list中的元素

groovy:000> ls1.get(5) # 获取索引为5的元素
===> 7
# 获取List中索引为i的元素,可以使用负数索引
groovy:000> l1[0]
===> a
# 获取List中索引为i的元素,可以使用负数索引
groovy:000> l1.getAt(0)
===> a
groovy:000> l1.getAt(-1)
===> 8
# 获取List中索引为i的元素,不能使用负数索引
groovy:000> l1.get(1)
===> b

5、获取子列表

groovy:000> ls1 = [1,2,3,4,5,6,7]
===> [1, 2, 3, 4, 5, 6, 7] # 重新定义一个ls1
groovy:000> ls1.subList(2,5) # 获取第 3 到 5 间的元素
===> [3, 4, 5]

6、是否包含元素3

groovy:000> ls1.contains(3)
===> true

7、获取首个元素和最后一个元素

groovy:000> ls1.first()
===> 1
groovy:000> ls1.last()
===> 7

8、获取数组长度

groovy:000> ls1.size()
===> 8

9、判断list是否为空

groovy:000> ls1.empty
===> false

弹出和压入元素(push pop)

1、pop 从此列表中删除最后一个项目

groovy:000> ls1 = [1,2,3,4,5,6,7]
===> [1, 2, 3, 4, 5, 6, 7]
groovy:000> ls1.pop()
===> 7
groovy:000> ls1.pop()
===> 6
groovy:000> ls1
===> [1, 2, 3, 4, 5]

2、push 创建由原始元素和集合中指定的元素组成的新列表

groovy:000> ls1 = [1,2,3,4,5,6,7]
===> [1, 2, 3, 4, 5, 6, 7]
groovy:000> ls1.push(8)
===> true
groovy:000> ls1
===> [1, 2, 3, 4, 5, 6, 7, 8]

获取数组类型

groovy:000> ls1.getClass()
===> class java.util.ArrayList

数组复制

1、l1赋给l2 无论修改l1或者l2 两者都会变化,底层同一份数据

groovy:000> l1 = ["a","b","c","d","e","f",1,2,3,4,5,6]
===> [a, b, c, d, e, f, 1, 2, 3, 4, 5, 6]
groovy:000> l2 = l1
===> [a, b, c, d, e, f, 1, 2, 3, 4, 5, 6]

数组迭代

// 定义一个水果类的数组
fruits = ["apple","Cherry tomato","Cherry","Custard","Dates","Grape"]

// 迭代,取值,不要索引
fruits.each{val -> println val}

// 迭代,取值,带索引
fruits.eachWithIndex{val,index -> println index + "::" + val}

# 示例如下
//  定义一个水果类的数组
groovy:000> fruits = ["apple","Cherry tomato","Cherry","Custard","Dates","Grape"]

// 迭代,取值,不要索引
groovy:000> fruits.each{val -> println val}
apple
Cherry tomato
Cherry
Custard
Dates
Grape

// 迭代,取值,带索引
groovy:000> fruits.eachWithIndex{val,index -> println index + "::" + val}
0::apple
1::Cherry tomato
2::Cherry
3::Custard
4::Dates
5::Grape

List的查找和过滤

// 定义一个List
groovy:000> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
===> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

// 查找大于5的元素
groovy:000> numbers.findAll { it > 5 }
===> [6, 7, 8, 9, 10]

//  查找小于5的元素
groovy:000> numbers.findAll { it < 5 }
===> [1, 2, 3, 4]

// 查找等于5的元素
groovy:000> numbers.findAll { it == 5 }
===> [5]

// 查找第一个大于5的元素,这是是find 不是findAll
numbers.find { it > 5 }
===> 6

// 查找是否存在大于10的元素
numbers.any { it > 10 }
===> false

// 查找所有的偶数
groovy:000> numbers.findAll { it % 2 == 0 }
===> [2, 4, 6, 8, 10]

索引

groovy:000> l3 = ['a','b','c','d','c']
===> [a, b, c, d, c]

// 返回index
groovy:000> l3.indexOf("c")
===> 2

// index返回-1意味着没有找到结果
groovy:000> l3.indexOf("z")
===> -1

// 返回最后一个索引
groovy:000> l3.lastIndexOf("c")
===> 4

判断

List.every{条件}:如果List中每一个元素都符合条件就返回true,否则为false

List.any{条件}:如果List中有一个元素符合条件就返回true,否则为false

groovy:000> l1 = [1,2,3,4,5,6]
===> [1, 2, 3, 4, 5, 6]

// 如果每一个元素都符合条件则返回true
groovy:000> l1.every{it < 7}
===> true

// 如果每一个元素都符合条件则返回true
groovy:000> l1.every{it < 6}
===> false

// any 如果有一个元素符合条件就返回true
groovy:000> l1.any{it > 5}
===> true

// any 如果有一个元素符合条件就返回true
groovy:000> l1.any{it > 3}
===> true

posted @ 2024-06-02 20:38  Ykubernetes  阅读(386)  评论(0编辑  收藏  举报