4、数组的数组
var weekDays = ["Mon","Tue","Wed","Thur","Fri"]; var days = [weekDays, weekDays]; System.out.println(days[1]);
结果:可能和你想的不一样,不是 weekDays ,而是 Tue
5、引用数组中的一个元素
var nums = [1..5]; System.out.println(nums[3]);
输出结果应该是:4
6、向数组中插入一个元素
第一种情况:默认,插入到最后
var nums = [1..5]; insert 0 into nums; System.out.println(nums);
结果:[ 1, 2, 3, 4, 5, 0 ]
0被插到最后位置上 第二种情况:插入到指定项之前
var nums = [1..5]; insert 0 before nums[3]; System.out.println(nums);
把 into 改成 before ,并指定参照位置就可以了
结果:[ 1, 2, 3, 0, 4, 5 ]
第三种情况:插入到指定项之后
var nums = [1..5]; insert 0 after nums[3]; System.out.println(nums);
把 before 改成 after 就可以了
结果:[ 1, 2, 3, 4, 0, 5 ]
7、删除数组中的元素
第一种情况:删除指定位置元素
var nums = [1..5]; delete nums[3]; System.out.println(nums);
结果:[ 1, 2, 3, 5 ]
第二种情况:删除指定值的元素
var nums = [1,2,3,3,4,5]; delete 3 from nums; System.out.println(nums);
结果:[ 1, 2, 4, 5 ]
两个 3 都被删除掉了
第三种情况:删除指点范围内的所有元素
var nums = [1..5]; delete nums[1..3]; System.out.println(nums);
结果:[ 1, 5 ]
注意:第一行里面的 1 代表 第 0 个元素值为 1 ;第二行里面的 1 代表第 1 个元素,他的值应该是 2
第四种情况:删除全部元素
var nums = [1..5]; delete nums; System.out.println(nums);
结果:[ ]
8、数组倒置
var nums = [1..5]; System.out.println(reverse nums);
结果:[ 5, 4, 3, 2, 1 ]
9、返回数组的子集
返回a 、b之间(含 a 和 b)的所有元素
def days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]; def weekend = days[5..6]; 结果:[ Sat, Sun ]
返回a 、b之间(含 a,不含 b)的所有元素
def days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]; def weekdays = days[0..<5];
结果:[ Mon, Tue, Wed, Thu, Fri ]
返回从 a 到序列末尾处的所有项
def days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]; def weekend = days[5..];
结果:[ Sat, Sun ]
返回 a 到序列末尾处的所有项(但不含最后一项)
def days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]; def days2 = days[0..<];
结果:[ Mon, Tue, Wed, Thu, Fri, Sat ]
10、用条件表达式选择子集
def nums = [1..9]; def numsGreaterThanTwo = nums[n | n > 2 and n!=6];
结果:[ 3, 4, 5, 7, 8, 9 ]
|