Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了数组 – Groovy,如何用索引迭代列表大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
使用Groovy中所做的所有简单的方法在访问一个迭代索引时,必须有一个更简单的方法来遍历列表。

for(i in 0 .. list.size()-1) {
   println list.get(i)
}

一个基本的for循环中没有隐含的索引?

for( item in list){
    println item       
    println index
}

解决方法

您可以使用 eachWithIndex

list.eachWithIndex { item,index ->
    println item
    println index
}

使用Groovy 2.4和更新版本,您也可以使用indexed()方法。这可以方便地使用诸如collect的方法访问索引:

def result = list.indexed().collect { index,item ->
    "$index: $item"
}
println result

大佬总结

以上是大佬教程为你收集整理的数组 – Groovy,如何用索引迭代列表全部内容,希望文章能够帮你解决数组 – Groovy,如何用索引迭代列表所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。