`
lijunaccp
  • 浏览: 153566 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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("]}")
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics