末尾追加
size()数组长度
复制代码
1
2
3
4
5
6
7
8
9
10
11class Example { static void main(String[] args) { def list1 = [] ; def list2 = [1,2,3,4]; list2.add(12); list2.add(12); println(list2.size()); println(list2); } }
6
[1, 2, 3, 4, 12, 12]
contains 包含元素返回为true
复制代码
1
2
3
4
5
6
7class Test{ static void main(String[] args){ def list2 = [1,2,3,4]; println(list2.contains(1)); } }
返回值是true
get (int number)获取元素的位置
复制代码
1
2
3
4
5
6
7class Test { static void main(String [] args){ String a = [1,2,3]; println(get(1)); } }
打印值是2,获取索引值为1的value
isEmpty是否为空
复制代码
1
2
3
4
5
6
7
8
9class Example { static void main(String[] args) { def lst = [11, 12, 13, 14]; def emptylst = []; println(lst.isEmpty()); println(emptylst.isEmpty()); } }
false
true
minus数组中删除数组,删除不存在的不会报错
复制代码
1
2
3
4
5
6
7
8class Test{ static void main(String[]args){ def list1 = [1,3,4,45,9]; deef list2 = 1list.minus([1,3,4,2]); println(list2 ); } }
[45, 9]
plus function which is used to add a new array to the existing array.
复制代码
1
2
3
4
5
6
7
8
9
10class Test{ static void main(String []args){ def list1 = [1,2,3,5]; def list2 = list1.plus([4,6]); println(list1); println(list2); println(list2.sort()); } }
[1, 2, 3, 5]
[1, 2, 3, 5, 4, 6]
[1, 2, 3, 4, 5, 6]
pop删除最后 一个元素
复制代码
1
2
3
4
5
6
7
8
9class Test{ static void main(String []args){ def list1 = [1,2,3,5]; def value= list1.pop();//返回删除的值 println(list1);// println(value); } }
[1,2,3]
5
remove删除指定索引的值
复制代码
1
2
3
4
5
6
7
8
9
10class Test{ static void main(String [] args){ def ar = [1,3,4,5]; def v = ar.remove(1);//返回删除的值 println(ar); println(ar.reverse()); println(v); } }
[1, 4, 5]
[5, 4, 1]
3
最后
以上就是温柔翅膀最近收集整理的关于Groovy列表的全部内容,更多相关Groovy列表内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复