java - Having trouble with multidimensional array initialization -
I'm trying to learn Java and of course I'm starting to have a problem of starting multidimensional array Here's the code I'm trying to face that I'm trying to write ...
import java.util.Scanner; Public square is a {public static zero major (string algos []) {int p [] [] = null; System.out.println ("Type ur array here:"); System.out.println ("how many rows and columns:"); Int line, colmn; Scanner u = new scanner (System.in); Scanner y = new scanner (System.in); Scanner T = New Scanner (System.in); Scanner r = new scanner (System.in); Line = t. NXIT (); Colmn = r.nextInt (); (Int i = 0; i & lt; = line; i ++) {int j = u.nextInt (); for int (v = 0; v & lt; = colmn; v ++); P [row] [colmn] = j; } Int a [] [] = p; System.out.println ("given array:"); Y (a); } Public static zero y (int n [] []) {for (int i = 0; i & lt; n.length; i ++) {for (int j = 0; j & lt; n [i] Length; j ++) {system .out.print (n [i] [j]); } System.out.println (); }}}
Please correct someone and give me enough knowledge which I need. Changes to the code are described in the comments, but please see the following code:
import java.util.Scanner; Public square is a {public static zero major (string algos []) {int p [] [] = null; System.out.println ("Type ur array here:"); System.out.println ("how many rows and columns:"); Int line, colmn; Scanner u = new scanner (System.in); // Only one needs to read from standard input stream, instead of: // scanner y = new scanner (System.in); // scanner T = new scanner (System.in); // scanner r = new scanner (System.in); // Usage scanner object "U": line = u.nextInt (); Colmn = u.nextInt (); // Memory in the array: P = new int [row] [column]; // replace '& lt; = 'To' & lt; ' Because the arrays are 0 indices and the index will be excluded from the exception limit; // or change 'p = new int [row] [column];' 'P = New Ent [Row + 1] [Coleman + 1];' (Int v = 0; v and lt; colman; v ++) {int j = u.x} (for i & lt; line; i ++;); Change the // index to "i, v" instead of "row, column": p [i] [v] = j; }} // The wrong way to copy the array as the same reference is being used. // To copy the array, use the following: / * * // int a [] [] = p; * Int [] [] A = new int [row] [colmn]; * For (int i = 0; i
Comments
Post a Comment