Klaus
Klaus

Reputation: 35

What is the groovy way for doing this initialization?

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'

Upvotes: 0

Views: 119

Answers (3)

Justin Piper
Justin Piper

Reputation: 3274

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

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340863

Based on ccheneson code:

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

Upvotes: 4

ccheneson
ccheneson

Reputation: 49410

This could do:

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

Upvotes: 2

Related Questions