我是靠谱客的博主 伶俐蜡烛,这篇文章主要介绍grails中遍历时的索引,现在分享给大家,希望可以做个参考。

例:
List<String> tests=new ArrayList<String>()
tests.add ('test1')
tests.add ('test2')
tests.add ('test3')
StringBuffer buf=new StringBuffer()
buf.append("{items:[")
int i=0
tests.each {
if(i>0){
println ","
}
buf.append("{test:")..append("'").append(it).append("'}")
i++
}
buf.append("]}")
以上是通过变量i来控制索引号的,以下可以直接利用grails中遍历自带的方法获得索引:
List<String> tests=new ArrayList<String>()
tests.add ('test1')
tests.add ('test2')
tests.add ('test3')
StringBuffer buf=new StringBuffer()
buf.append("{items:[")
tests.eachWithIndex {
test,i->
if(i>0){
println ","
}
buf.append("{test:").append("'").append(test).append("'}")
}
buf.append("]}")

最后

以上就是伶俐蜡烛最近收集整理的关于grails中遍历时的索引的全部内容,更多相关grails中遍历时内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部