想象一下,我有一个很大的列表,我想分成更小的块进行处理:
def chunkSize = 10
def listSize = aBigList.size()
for(def i = 0; i < listSize; i += chunkSize) {
def startOfRange = i
def endOfRange = (listSize - 1) < (i + increment - 1) ? (listSize - 1) : (i + increment - 1) // There has got to be a better way!
def chunk = aBigList[startOfRange..endOfRange]
// do something with chunk
}
获取endOfRange
变量的代码是丑陋且非常规的,但为了防止Index Out of Bounds
异常是必需的。有一个更好的方法吗?