[英]Combining elements in jagged 2D arrays into one new jagged 2D array (Deep Copy issue)
Given two jagged arrays: a & b where a + b will always have the same # of rows: 给出两个锯齿状数组:a&b其中a + b将始终具有相同的行数:
int[][] a = { {1,2}, {4,5,6} };
int[][] b = { {7}, {8,9,0} };
how exactly can I manipulate a new jagged array c to return: { {1,2,7}, {4,5,6,8,9,0} }
? 我怎么能操纵一个新的锯齿状数组c来返回: { {1,2,7}, {4,5,6,8,9,0} }
?
Here's what I have so far: 这是我到目前为止所拥有的:
int[][] c = null;
for(int i = 0; i<a.length; i++){
c = new int[a.length][a[i].length + b[i].length];
}
//rest of my code for assigning the values into the appropriate position works.
The trouble arises, as you all can see, that I am performing a deep copy, which, on the second iteration of the for-loop, is setting ALL rows to a length of the length of the current row on the step of the iteration. 正如你们所看到的那样,麻烦出现了,我正在执行一个深层复制,在for循环的第二次迭代中,它将所有行设置为迭代步骤中当前行长度的长度。
You are creating a new 2D array object each iteration of your loop. 您正在循环的每次迭代中创建一个新的2D数组对象。 Each time through, you are reassigning c
, thus throwing out all of your previous work. 每次通过,你都会重新分配c
,从而抛弃你以前的所有工作。 Additionally, placing a number in both set of brackets at the same time results in each row having the same length . 另外,在两组括号中同时放置一个数字会导致每行具有相同的长度 。
Using your example, the first time through the loop, c
is assigned to a 2D array with two rows, each of length three. 使用您的示例,第一次通过循环时, c
被分配给具有两行的2D数组,每行长度为3。 The second time through the loop, you throw out your previous 2D array and create a new one having two rows, each of length six. 第二次循环,你抛出你以前的2D数组并创建一个有两行的新数组,每行长度为6。
But what you need to be doing is creating a new row each time through the loop, not the entire 2D array. 但是你需要做的是每次通过循环创建一个新行,而不是整个2D数组。
First, we create a 2D array called c
and specify that it has a.length
rows. 首先,我们创建一个名为c
的2D数组,并指定它具有a.length
行。 We don't put a value in the second bracket, because that would indicate that all of the rows are of the same length. 我们不在第二个括号中放置一个值,因为这表明所有行的长度都相同。 So at this point, c
does not know about row length. 所以在这一点上, c
不知道行长度。 It just knows how many rows it can have. 它只知道它可以有多少行。 Keep in mind: c
doesn't actually have any rows yet, just a capacity for a.length
rows. 请记住: c
实际上还没有任何行,只是a.length
行的容量。
Next, we must create the rows and assign a length/capacity to them. 接下来,我们必须创建行并为它们分配长度/容量。 We set up our loop to run as many times as there are rows. 我们将循环设置为与行一样多次运行。 The current row index is denoted by i
, and therefore, c[i]
refers to a specific row in the 2D c
array. 当前行索引由i
表示,因此, c[i]
指的是2D c
数组中的特定行。 We use new int[]
to create each individual row/array, but inside the brackets, we must specify the length of the current row. 我们使用new int[]
创建每个单独的行/数组,但在括号内,我们必须指定当前行的长度。 For any row c[i]
, its length is given by the sum of the lengths of a[i]
and b[i]
; 对于任何行c[i]
,其长度由a[i]
和b[i]
的长度之和给出; that is, a[i].length + b[i].length
. 即, a[i].length + b[i].length
。
What we are left with is an array c
that contains rows/arrays, each with a set length/capacity that matches the sum of the corresponding rows lengths in a
and b
. 我们剩下的是一个包含行/数组的数组c
,每个行/数组的设置长度/容量与a
和b
相应行长度的总和相匹配。
Keep in mind that c
still does not contain any integer values, only containers that are of the correct size to hold the values in a
and b
. 请记住, c
仍然不包含任何整数值,只有大小正确的容器才能容纳a
和b
的值。 As you mentioned, you already have code to populate your array with values. 正如您所提到的,您已经有了使用值填充数组的代码。
int[][] c = new int[a.length][];
for (int i = 0; i < a.length; i++) {
c[i] = new int[a[i].length + b[i].length];
}
When initialize Java 2D array, lets consider it as a table; 初始化Java 2D数组时,我们将其视为一个表; you only have to give the number of rows and in each row of your table can have different number of columns. 您只需要给出行数,并且在表的每一行中可以有不同数量的列。
Eg. 例如。 Say we have a 2D array call c defined as follows, 假设我们有一个2D数组调用c定义如下,
int[][] c = new int[10][];
It says you defined c contains 10 of int[] elements. 它说你定义了c包含10个int []元素。 But in order to use it you have to define the number of columns each row has. 但是为了使用它,你必须定义每行的列数。
Eg. 例如。 Say we have 3 columns in the second row 假设我们在第二行有3列
int c[1] = new int[3];
So in this example you have to add the column values of 2D arrays a
and b
to calculate the resultant array which is c
. 因此,在此示例中,您必须添加2D数组a
和b
的列值以计算结果数组,即c
。
c[i] = new int[a[i].length + b[i].length];
This will give you what you expected. 这将给你你所期望的。
int[][] a = { {1,2}, {4,5,6} };
int[][] b = { {7}, {8,9,0} };
int[][] c = new int[a.length][];
for(int i = 0; i<a.length; i++){
c[i] = new int[a[i].length + b[i].length];
for (int j=0;j< a[i].length; j++) {
c[i][j] = a[i][j];
}
int length = a[i].length;
for (int j=0;j< b[i].length; j++) {
c[i][length+j] = b[i][j];
}
}
尝试c[i] = new int[a[i].length + b[i].length]
int[][] c = new int[a.length][];
for(int i = 0; i < c.length; i++){
c[i] = new int[a[i].length + b[i].length];
int x = 0;
for (int num : a[i]) {
c[i][x] = num;
x++;
}
for (int num : b[i]) {
c[i][x] = num;
x++;
}
}
or even simpler... 甚至更简单......
int[][] c = new int[a.length][];
for(int i = 0; i < c.length; i++){
c[i] = new int[a[i].length + b[i].length];
System.arraycopy(a[i], 0, c[i], 0, a[i].length);
System.arraycopy(b[i], 0, c[i], a[i].length, b[i].length);
}
Try this one: 试试这个:
int[][] c = new int[a.length][];
for(int i = 0; i<a.length; i++){
c[i] = new int [a[i].length + b[i].length];
int j;
for(j=0; i < a[i].length; j++){
c[i][j] = a[i][j];
}
for(int k=0; i < b[i].length; k++){
c[i][j+k] = b[i][j];
}
}
public static void main(String [] args) { public static void main(String [] args){
int[][] a = { {1,2}, {4,5,6} };
int[][] b = { {7}, {8,9,0} };
int[][] c = null;
for(int i = 0; i<a.length; i++){
c = new int[a.length][a[i].length + b[i].length];
}
for(int i = 0; i<a.length; i++){
for (int j = 0; j < a[i].length+b[i].length; j++) {
if(j< a[i].length){
c[i][j]=a[i][j];
}
if(j< a[i].length+b[i].length && j>= a[i].length){
c[i][j]=b[i][j-a[i].length];
}
}
}
for(int i = 0; i<a.length; i++){
for (int j = 0; j < a[i].length+b[i].length; j++) {
System.out.print(c[i][j]);
}
System.out.println();
}
} }
This works in my system ........... 这适用于我的系统...........
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.