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

Popular posts from this blog

Change the color of an oval at click in Java AWT -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -