How to Print a specific point from an array from another method ? #Java -
public class newtest { public static void main(string[] args) { string [][] table; table = new string [4][4]; for(int y=0; y<4; y++){ for(int x=0; x<4; x++){ table[y][x] = " ~ " ; } } int y, x; for(y=0; y<4; y++) { system.out.print(y+": "); for(x=0; x<4; x++) system.out.print(table[y][x]+" "); system.out.println(); } } public void table () { system.out.println(table[2][2]); } }
//this line have problems ! system.out.println(table[2][2]);
the problem string [][] table
local method declared, , is, therefore, invisible other methods of class.
there 2 ways of making visible:
- make
string [][] table
static
member in enclosing class (becausemain
static
), or - pass
string [][] table
function parameter.
the second solution better:
// here declaration of method taking 2d table public static void showtablecell(string [][] table) ... ... // here call of method main(): showtablecell(table);
Comments
Post a Comment