Reputation: 2899
Ok im trying to loop through a 2d array to copy from one to another. I can assign the integers from the first part using:
array1[i] = array2[i];
but I cannot do the same for the other part e.g:
array1[][j] = array2[][j]; //this doesn't compile
OR
array1[0]j = array2[0][j]; //creates run time error
How can I specfically copy the second part but not the first?
Upvotes: 4
Views: 26746
Reputation: 489
In addition to what has already been said here, which are all good answers, try looping through the array via two separate for loops, one for-loop for the first dimension, and another for the second dimension. If you want all of the 'j' values in the 2nd row you can merely do
for(int j = 0; j < yourJDimension; j++) {
someValue = yourArray[1][j];
}
This can help show you the contents of your array, and how it is structured. Also, here is a quick link that might be somewhat useful:
http://www.leepoint.net/notes-java/data/arrays/arrays-2D.html
Upvotes: 0
Reputation: 165
If the arrays a and b are the same size, this will copy b into a element by element
for(int i = 0; i < rows; i++)
for(int j = 0; j < columns; j++)
a[j][i] = b[j][i];
Your question doesn't really make sense though. You can't copy the "second part" of a matrix. Think of the definition:
int a[5][5];
a is an array of arrays. a[0] is not an int, it's an int array. a[0][0] however is the first element of the first array, making it an int.
Upvotes: 0
Reputation: 11475
Try using this class which does the job of copying arrays: java.util.Arrays
Upvotes: 0
Reputation: 13624
There is no 'first part' and 'second part' of a 2-dimensional array. There are i
rows and j
columns in an array declared x[j][i]
.
Now for the technical side:
Java does not have true 2-dimensional arrays; it has arrays of arrays, so x[rows][cols]
is an array x
of rows
arrays of cols
elements (i.e. x[rows]
is an array of arrays).
So when you performed array1[i] = array2[i]
, you were copying references to the arrays of columns in the second array.
But there is no way to do the mirror image of that -- you cannot copy references to the rows but keep the column values, because the array of rows is x
.
If you are looking for a "deep copy", you can do it manually with:
for (int row = 0; row < array1.length; row++) {
for (int col = 0; col < array1[row].length; col++) {
array2[row][col] = array1[row][col];
}
}
Upvotes: 6
Reputation: 1500515
It's not really clear what you mean. Bear in mind that a "2d array" is really just an array of arrays. Your first snippet of code is just copying the 'reference to the nested array' in array2
to array1
.
So what is the "second part" here? Are you trying to copy a "column" from one array to the other? If so, just remember: there are no columns. There are just arrays, and arrays of arrays, and so on. If all the "subarrays" happen to have the same size, that's somewhat coincidental. There's nothing to stop you from doing:
array1[0] = new int[100];
array1[1] = new int[5];
You can think of "rows" (each being an array) but what would "column 10" mean in the above example?
Upvotes: 3