简体   繁体   English

JAVA:从for循环中的1d数组初始化2d数组

[英]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. 我已经阅读了有关此主题的线程,但是无法解决在for循环中使用多个1d数组初始化2d int数组时遇到的问题。

Below is my code snippet which does following: 下面是我的代码片段,它执行以下操作:

  1. Reads a line in string from std input. 从std输入读取字符串中的一行。
  2. Convert this line into 1d int array (by splitting the string) 将此行转换为1d int数组(通过拆分字符串)
  3. Populate the rows of 2d array with each 1d array sequentially 依次用每个1d数组填充2d数组的行

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: 我面临的问题是,每次使用新的1d数组值覆盖2d数组的值:以下是示例输出:

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. 请提出建议,如果有更好的方法用1d数组初始化2d数组。

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. 您的问题是row_int变量的内存在每个循环中都被重用。 Hence, each time you store values in the row_int array, you're setting values to all of its references too. 因此,每次将值存储在row_int数组中时,也将为其所有引用设置值。 You need to reinitialize them for each loop. 您需要为每个循环重新初始化它们。 That should sort out the issue. 那应该解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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