我有一个数组数组。
def my_array = [[null], [10382], [11901], [null], [10386], [10385], [11901], [10386], [11901], [10386], [3], [null], [10504], [3]]
我想找到元素第一次出现的索引,例如[3]
。
我正在使用findIndexOf
.
def index = my_array.findIndexOf { it == [3] }
但是,这将返回-1
。我认为这是 array[3]
中元素的索引而不是 array my_array
。如何获取元素的索引[3]
中my_array
?
运行此代码:
def my_array = [[null], // index 0
[10382], // index 1
[11901], // index 2
[null], // index 3
[10386], // index 4
[10385], // index 5
[11901], // index 6
[10386], // index 7
[11901], // index 8
[10386], // index 9
[3], // index 10
[null], // index 11
[10504], // index 12
[3]] // index 13
def index = my_array.findIndexOf { it == [3] }
println "index: $index"
这与您的代码结果相同:
~> groovy solution.groovy
index: 10
~>
在 java 8 和 groovy 2.1.16 上。换句话说,您的代码应该可以工作。
作为旁注,您真的需要列表列表吗?由于所有列表的长度都是 1,您不妨直接拥有一个元素列表。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句