1

I'm porting some JavaScript to Java and I'm having a hard time understanding two lines of the following piece of code:

var blocks=[];
for (var z=0; z<size; z++) {
    var slice=blocks[z]=[];    //Those are the lines I don't understand.
    for (var x=0; x<size; x++) {
        var row=slice[x]=[];    //Those are the lines I don't understand.
        for (var y=0; y<size; y++) {
            row[y]=isFull(x,y,z);
        }
    }
}

The first line is declaring "slice", then it assigns "blocks[z]", and then again it assigns an empty array.

As I'm writing this it came to my head that maybe is for clearing any previous info before assigning new data, but I'm not sure.

Deses
  • 1,074
  • 2
  • 12
  • 25

3 Answers3

1

Actually an empty array is assigned to blocks[z], and the value of blocks[z] (the empty array) is then assigned to slice.

Basically it's just a short way of assigning a value to two (or more) variables

nice ass
  • 16,471
  • 7
  • 50
  • 89
  • So `slice` and `row` are always full of empty arrays, no matter what is in `blocks[z]`, isn't it? – Deses Feb 07 '13 at 00:07
  • 1
    well by the time the loop ends, `slice` should be an array with empty arrays, but `row` will be an array with items that take the return value of your `isFull` function (you should do a `console.log(slice, row)` and you'll see the values) – nice ass Feb 07 '13 at 00:11
  • Ohhhh... Now that I see the output of everything I understand it! Thank you. – Deses Feb 07 '13 at 00:21
1

yes and no, it would clear previous data, but that's what var is doing anyways. The important part is that it assigns an array so that the following lines don't crash

blocks =[]; // blocks is an array of zero length 'filled' with NULL values;
var slice = blocks[z]; // slize would be the same as blocks[z] so it'd be NULL
blocks[z] = []; // blocks[z] is now an zero length array filled with NULL values.

All assignment code is executed from right to left so it first assigns an array to blocks[z] and assigns that same array to var slice

and so forth

itsid
  • 801
  • 7
  • 16
0
x = y = z;

is exactly equivalent to:

y = z;
x = y;

in that order.

Michael Day
  • 1,007
  • 8
  • 13