Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

System.arraycopy()

Status
Not open for further replies.

Chrissirhc

Programmer
May 20, 2000
926
0
0
GB
Hello
I want to know how to use arraycopy to copy a two dimensional array. (there are large amounts of data)
I can copy the 2D array manually by using two for loops. But does anyone know if using the arraycopy method is there just for convinience or has it got performance advantage and if so how do I do it.

Thanks in advance
Chris
 
this is from sun.
Use System.arraycopy() to improve performance. This is a native method, and much faster than manual array processing.

Here it is. :)
 
There is no sample code


int[][] array1 = new int[][] { {0, 1 }, {2, 3 } }; //Your array int[][] array2=new int[array1.length][]; //to be copied

for (int i=0;i<array2.length;i++ ){
array2 = new int[array1.length];
System.arraycopy(array1, 0, array2, 0, array2.length);
}

is this right?
It seems a little odd especially the first line the for loop. We keep on resetting the array2.
Chris
 
no ! arraycopy should be OUTSIDE the for loop as it copies one array into the other !!!!!!! so what you're doing is : for each element of array 1, copy array1 in array2 ... you won't improve any performance ;] !
 
Could you send me some sample code please. Bear in mind I am copying 2D arrays [][] and they are very large
say for instance [1][1500] is a legal array element as is
[2000][1500] now you probably can see there is a lot to copy and that is why I want to do it efficiently. I am sorting of guessing that I'd have to for loop 2000 times and copy an array into each element of the 2000. (here I would use System.arraycopy().

Thanks in advance

Chris

 
Dear All,

I guess I don't understand what the problem is. Here is the code that I run which has no problems copying the 'matrix' of integers.


protected void displayMatrix(int[][] matrix){
for(int nRow=0; nRow<matrix.length; nRow++){
for(int nCol=0; nCol<matrix[nRow].length; nCol++){
System.out.print(matrix[nRow][nCol] + &quot;,&quot;);
}
System.out.println();
}
} // displayMatrix()


protected int[][] copyMatrix(int[][] matrix){
int[][] mRet = new int[matrix.length][matrix[0].length];
System.arraycopy( matrix,0,mRet,0,matrix.length );
return mRet;
}

// use the functions above
int[][] iA1 = new int[5][5];
for( int nRow=0; nRow<iA1.length; nRow++){
int[] cols = iA1[nRow];
for(int nCol=0; nCol<cols.length; nCol++){
cols[nCol] = nRow * nCol;
}
}
displayMatrix( iA1);
displayMatrix(copyMatrix(iA1));


Confused
-pete
 
i think you're right pete
i only posted to say : putting arraycopy INSIDE a for loop is a nonsense !!!!
 
I did not write the code I posted it was from another forum and I was sceptical of it so I posted it here. His understanding of arraycopy contradicts yours. I think that he thought that arraycopy can only copy 1 dimensional arrays. You assume that it makes a perfect copy. Perhaps looking at the code above you will see that he is sort of copying one array into the element of another. So thats why arraycopy is inside the for loop.Infact many people in that forum posted similar code. (Java.sun.com). Maybe they are assuming that &quot;matrix[0].length&quot; changes. For my program it does not.

protected int[][] copyMatrix(int[][] matrix){
int[][] mRet = new int[matrix.length][matrix[0].length];
System.arraycopy( matrix,0,mRet,0,matrix.length );
return mRet;
}

Thanks I will try this code

Chris

 
Chris,

> You assume that it makes a perfect copy.

I did not assume anything. The code I posted was executed before I posted it!

If I ever post code without running it (does not happen often) to verify correct operation my post will state so in no uncertain terms.

-pete
 
Okay your right.

My apologies.

Thanks for your help

Chris
 
Chris,

> My apologies.

No need to apologize, I did not take offense to your statment, my post about 'assuming' was only meant to let people know that I don't generaly post code samples that I haven't executed for verification. I'm sorry if my post sounded abrupt. Cheers ;-)

-pete
 
I have implemented the code you sent and unless I have implemented it incorrectly it doesn't work for me. I know this is I display and image of my array and it comes blank after using the arraycopy method this is the code I originally had and I have commented out the code which I used that did not work for me. Maybe you can see my error or better understand what I'm trying to do thanks Chris.

/**
* This method copies a 2D array.
* @param arrayToCopy the array we want to copy
* @return theClone a copy of arrayToCopy.
**/
public byte[][] clone2DArray(byte[][] arrayToCopy)
{
byte[][] theClone = new byte[arrayToCopy.length][arrayToCopy[0].length];

System.out.println(&quot; The width is &quot; + width + &quot; The array width is &quot; + arrayToCopy.length);
// Had to copy the arrays.
for (int y = 0; y < arrayToCopy[0].length ; y++)
{
for (int x = 0; x < arrayToCopy.length; x++)
{
theClone[x][y] = arrayToCopy[x][y];
}
//System.out.println(&quot;What y value we at? &quot; + y);
}

// System.arraycopy( arrayToCopy,0,theClone,0,arrayToCopy.length );
return theClone;
}

 
Chris,

I don't see anything wrong with your code. My small test class runs perfectly on Win2k in both the latest Microsoft VM and Sun JDK 1.3.

The one thing I will point out is that my copyMatrix() function assumes that each row has the same number of elements. If you need support for variant sized rows then you would need to add the additional code to new each row to the appropriate size.

Here is the complete class file that I used:


public class TTRnd
{
/**
* The main entry point for the application.
*
* @param args Array of parameters passed to the application
* via the command line.
*/
public static void main (String[] args)throws Exception
{
new TTRnd(Integer.parseInt(args[0]),
Integer.parseInt(args[1]));
}

public TTRnd( int rows, int cols)throws Exception {
int[][] matrix = new int[rows][cols];
for( int nRow=0; nRow<rows; nRow++){
for( int nCol=0; nCol<cols; nCol++){
matrix[nRow][nCol] = nRow * nCol;
}
}

displayMatrix(matrix);
displayMatrix( copyMatrix(matrix));

System.in.read();
}

static protected int[][] copyMatrix( int[][] matrix){
int[][] mRet = new int[matrix.length][matrix[0].length];
System.arraycopy( matrix,0,mRet,0,matrix.length );
return mRet;
}

static protected void displayMatrix(int[][] matrix){
for(int nRow=0; nRow<matrix.length; nRow++){
for(int nCol=0; nCol<matrix[nRow].length; nCol++){
System.out.print(matrix[nRow][nCol] + &quot;,&quot;);
}
System.out.println();
}
}
}
 
That was what I sort of meant by assumption of perfect copy, but I wasn't clear on this. But anyway as I stated I am using rectangle(well an image) so its bounds are a rectangle. I don't understand why the code works for you and not for me. Only difference is that I am using a byte array but the API just says object so I am not understanding this. In fact I am more confused because if its making an exact copy of the array it shouldn't matter that the no of arrays in a array changes.

Cheers

Chris
 
Chris,

I have no idea what your development experience is, so it is difficult to guess where you should look for the problem.

It is possible that your problem is not in the code you are posting. None of your posts show your initialization of the source matrix. It could be that your source matrix has nothing in it which is why the copy then has nothing in it.

-pete
 
I know that the incomming array or 'source matrix' has something in it as the original code works. Using arraycopy
the returned array must have elements all 0 in it as the image made comes out white which implies this. Perhaps it is a case of me printing out the array just to check. The strange thing is that the method works properly when I copy elements indiviually but not when I use arraycopy. Or the should I say the end result works when I use for loops instead of arraycopy. So maybe as you say the problem exists somewhere else but I don't where. I would post the code for the whole class but it is too long (2000 odd lines).
From my understanding it seems that if I change a method that has a specific function and the final results change because I change the contents in that method then it must be the method at fault especially in this case where all it does is return a copy of another array.

Thanks

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top