Quantcast
Channel: 配列タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 757

Groovyで配列からn個づつ取り出す

$
0
0

コード

def domain=['out','open','vid','uuu','rrrr','tttt','kkk','rrr','ppp','rfde','wsaq']

def splitCount = 5

for (i in 0..Math.ceil(domain.size() / splitCount) -1 ) {  

    //開始index
    def firstIndex = i == 0 ? i : i * splitCount

    //終了index
    def lastIndex = (firstIndex + splitCount - 1) < domain.size() - 1 ?  (firstIndex + splitCount - 1) : domain.size() - 1

    print "start:" + firstIndex + "\n"
    print "end:" + lastIndex + "\n"

    print "get list:" + domain[firstIndex..lastIndex] + "\n"

}

結果

start:0
end:4
get list:[out, open, vid, uuu, rrrr]
start:5
end:9
get list:[tttt, kkk, rrr, ppp, rfde]
start:10
end:10
get list:[wsaq]

Viewing all articles
Browse latest Browse all 757

Trending Articles