1
我在我的控制器中有以下代碼。我如何訪問每個索引?每個Grails索引
def arr = ['a', 'b', 'c']
arr.each
{
// 'it' is the element
println it
}
我在我的控制器中有以下代碼。我如何訪問每個索引?每個Grails索引
def arr = ['a', 'b', 'c']
arr.each
{
// 'it' is the element
println it
}
您可以使用eachWithIndex:
arr.eachWithIndex { obj, i ->
println "${i}: ${obj}"
}
有一些基本的文檔,這將有助於你解決各種關於常規集合和映射的問題 - 看看在http://groovy.codehaus .org/For + those + new + to + both + Java +和+ Groovy這是一個很好的總結,並且不會浪費時間將所有內容都返回給Java。 http://groovy.codehaus.org/JN1015-Collections http://groovy.codehaus.org/JN1025-Arrays http://groovy.codehaus.org/JN1035-Maps – Anarchofascist