java - For loop iterating through table -
say created table
char[][] table = new char[5][5];
and wanted iterate using for loop
creating "space."
for (int = 0; < table.length; i++) (int j = 0; j < table[i].length; j++) table[i][j] = ' ';
in second line [i] mean in table[i].length
? why can't table.length first line? thank
your declaration :
char[][] table = new char[5][5];
is equivalent :
// declare array of size 5, each element reference one-dimen char[] array char[][] table = new char[5][]; // initialize elements of table array, i.e. each row table[0] = new char[5]; table[1] = new char[5]; table[2] = new char[5]; table[3] = new char[5]; table[4] = new char[5];
note: have initialized each "row" arrays of different size, instance
table[3] = new char[255];
and table[1].length
5, while table[3].length
255.
these sizes of ["rows"] arrays independent of "aggregate" array size table.length
, therefore have loop thru each "row" using size of "row" array.
Comments
Post a Comment