Dat*_*Chu 4 collections groovy types
我一定是疯了,但为什么 Groovy findIndexValues 返回List<long>?我可以获得整数中的索引吗?
foo = ['a','b','d','e', 'e','e']
indices = foo.findIndexValues { it == 'e'}
indices.each { println foo[it] }
Run Code Online (Sandbox Code Playgroud)
上面的代码会崩溃,因为 foo 集合不能像访问索引那样处理 long。我是不是没有使用它应该使用的语言?
这就是该方法的工作原理。它使用迭代器遍历集合并跟踪匹配的索引为 long。理论上,它支持大于Integer.MAX_VALUE,尽管我怀疑这在实践中是否有用。
您可以使用以下方法解决它:
indices.each { println foo[it as int] }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1451 次 |
| 最近记录: |