例:
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中遍历时内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复