开发者

What is the groovy way for doing this initialization?

开发者 https://www.devze.com 2023-02-19 22:00 出处:网络
Hi what is the grrovy way of doing this kind of initialization? for(i=0; i<10; i++) for(j=0; j<20; j++)

Hi what is the grrovy way of doing this kind of initialization?

for(i=0; i<10; i++)
   for(j=0; j<20; j++)
      for(k=0; k<20; k++)
         m开发者_开发知识库[i][j][k]='a'


Based on ccheneson code:

10.times { i ->
    20.times { j ->
        20.times { k ->
            m[i][j][k] = 'a'
        }
    }
}


This could do:

(0..9).each { i ->
    (0..19).each { j ->
        (0..19).each { k ->
            m[i][j][k] = 'a'
        }
    }
}


Not sure how efficient this is. Concise though.

final m = new char[10][20][20]
for(i=0; i<10; i++)
   for(j=0; j<20; j++)
      for(k=0; k<20; k++)
          m[i][j][k]='a'

final n = [[['a'] * 20] * 20] * 10 as char[][][]

assert n == m
0

精彩评论

暂无评论...
验证码 换一张
取 消