java - Different looping behaviors in Groovy -
why these 2 loops have different results? thought both initialize of values in each array 5 second 1 works. explain why is?
static main(args) { double[][] x = new double[3][3] double[][] y = new double[3][3] for(row in x) { for(num in row) { num=5 } } for(int i=0;i<y.size();i++) { for(int j=0;j<y[i].size();j++) { y[i][j]=5 } } println "x: ${x}" println "y: ${y}" }
and here's output
x: [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] y: [[5.0, 5.0, 5.0], [5.0, 5.0, 5.0], [5.0, 5.0, 5.0]]
first pair of for
loops nothing , correct. num
new variable in scope of inner for
. reference integer table. when assign it, becomes reference value 5. table cell not change.
for c programmer.
int 5 = 5; int *num;
it is:
num = &five;
it not:
*num = five;
Comments
Post a Comment