Allocating 2 dimension array of a class Java -
It's just working fine.
test [] [] matrix = new exam [5] []; For (int i = 0; i <5; i ++) {matrix [i] = new test [5]; For (int j = 0; j and lt; 5; j ++) matrix [i] [j] = new test (); } It does not work
[to (Test] t: Matrix) {t = new trial [5]; For (test T2: T) T2 = new test (); } It is working
int [] [] matrix 2 = new Int [5] [5]; Without starting at all
Why is the question?
Because you are assiming for local variables and not Matrix . for (test [] t: matrix) {t = new test [5]; // You are assing for a local variable / / t is a local variable! } To make it more clear:
(int i = 0; i & lt; matrix.length; i ++) {Test [] t = matrix [i]; // t is clearly a local variable // It will specify a new array for local variables: T = new test [5]; // matrix [i] is still empty, to prove: system.out.println (matrix [i]); // print "empty"}
Comments
Post a Comment