简体   繁体   中英

JAVA : Initialising 2d array from 1d array in for loop

I have read threads on this topic but I was not able to solve the issue I am facing in initialising 2d int array with multiple 1d arrays in for loop.

Below is my code snippet which does following:

  1. Reads a line in string from std input.
  2. Convert this line into 1d int array (by splitting the string)
  3. Populate the rows of 2d array with each 1d array sequentially

Constraints :

  1. The Size of the input is known (number of rows and columns) and it will always be integer.

     Scanner in = new Scanner(System.in); String[] line = new String[8]; String[] rows = new String[7]; int[] row_int = new int[7]; int player=0,j=0; for (int i =0; i <3 ;++i) { line[i] = in.nextLine(); System.out.println("REading ..."+line[i]); rows = line[i].split("\\\\s+"); j=0; for (String s : rows) { row_int[j]=Integer.parseInt(s); j++; } input[i]=row_int; System.out.println(Arrays.toString(row_int)+" for the value of i "+i); System.out.println("The Array is \\n"+Arrays.deepToString(input)); } 

The issue I am facing is that the values of 2d array gets overwritten every time with the new 1d array values : Below is a sample output:

1 2 3 4
REading ...1 2 3 4
[1, 2, 3, 4, 0, 0, 0] for the value of i  0
The Array is 
[[1, 2, 3, 4, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0]]

4 5 6
REading ...4 5 6
[4, 5, 6, 4, 0, 0, 0] for the value of i  1
The Array is 
[[4, 5, 6, 4, 0, 0, 0], 
[4, 5, 6, 4, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0]]

33 44 55
REading ...33 44 55
[33, 44, 55, 4, 0, 0, 0] for the value of i  2
The Array is 
[[33, 44, 55, 4, 0, 0, 0], 
[33, 44, 55, 4, 0, 0, 0], 
[33, 44, 55, 4, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0]]

Indenting the output for better readability.

Please suggest, if there is a better way to initialise a 2d array with 1d array.

Scanner in = new Scanner(System.in);
String[] line = new String[8];
String[] rows = new String[7];


int player=0,j=0;

for (int i =0; i <3 ;++i) {
line[i] = in.nextLine();
System.out.println("REading ..."+line[i]);
rows = line[i].split("\\s+");
j=0;
int[] row_int = new int[7]; //reinitializing the variable on each loop
for (String s : rows) {
    row_int[j]=Integer.parseInt(s);
    j++;
}
input[i]=row_int;
System.out.println(Arrays.toString(row_int)+" for the value of i  "+i);
System.out.println("The Array is \n"+Arrays.deepToString(input));
}

Your problem is that the row_int variable's memory is reused on every loop. Hence, each time you store values in the row_int array, you're setting values to all of its references too. You need to reinitialize them for each loop. That should sort out the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM