def a = [[6, 4], [8, 6], [5, 3]]
a.findAll {
it.findAll {
it != 4 && it != 6
}
}
所以我编写了这段伪代码,这样我就不需要编写复杂的代码了。所以我想从数组中删除所有 4,但如果我只写 it != 4
然后它不会删除它,如果我像现在这样写两个数字 it != 4 && it != 6 然后它会删除它。我应该以不同的方式使用它吗?我想要这个数组
def a = [[6, 4], [8, 6], [5, 3]]
例如从中删除所有数字 4。
最佳答案
删除包含 4
的子列表
使用in
语法检查子列表内部。所以你的代码可以重写为:
def a = [[6,4],[8,6],[5,3]]
assert [[8, 6], [5, 3]] == a.findAll { !(4 in it) }
从每个子列表中删除 4
def a = [[6,4],[8,6],[5,3]]
// Modify list in place, to return a new list use collect
assert [[6], [8, 6], [5, 3]] == a.each{ it.removeAll { it == 4 } }
关于arrays - Groovy 过滤器二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34763164/