3

I would like to know what is the right way to copy two-dimension array from temporary array into original array. The array size is unknown at the beginning of the program and user is set it later on. here's the code:

private static void copy_Matrix(int origin_Matrix[][] , int copy_Matrix[][], int row, int column)

{

      for ( int i = 0 ; i < row-1; ++i)
      {
          for (int j = 0; j < column; j++)
          {
              origin_Matrix[i][j] = copy_Matrix[i][j]; 

          }
      }

}

I know it's wrong, please help..

0

4 Answers 4

3

No prefix increment necessary in Java. Also you don't need to pass a row and column count in Java (that only leaves room for error). And, there exists a function, System.arraycopy which should be at least as efficient as copying them one at a time, if not more so.

private static void copy_Matrix(int origin_Matrix[][] , int copy_Matrix[][])
{
    /* assert origin_Matrix.length == copy_Matrix.length */
    for ( int i = 0 ; i < origin_Matrix.length; i++)
    {
        /* assert origin_Matrix[i].length == copy_Matrix[i].length */
        System.arraycopy(origin_Matrix[i], 0, copy_Matrix[i], 0, origin_Matrix[i].length);
    }
}

Note that you should be aware of Java Collections (such as List/ArrayList or Vector) and consciously decide that the slight performance gains of an array of ints is necessary in your case, over the additional readability and convenience that the Collections provide.

Sign up to request clarification or add additional context in comments.

Comments

2

You'd better end with something like:

private static int[][] copy_Matrix(int origin_Matrix[][])
{
    int copy[][] = new int[origin_Matrix.length][origin_Matrix[0].length];
    for ( int i = 0 ; i < origin_Matrix.length; ++i)
    {
        System.arraycopy(origin_Matrix[i], 0, copy[i], 0, origin_Matrix[i].length);
    }
    return copy;
}

Comments

1

Is there a reason you are using primitives for this instead of Java Collections? If not, I would use those. They are optimized, and provide a fairly robust API for manipulation.

1 Comment

int arrays have their place in certain operations. For example when doing crazy Project Euler problems I was using Vector in place of an array because it was easier and I figured it was optimized enough that it shouldn't make a big difference; after being disappointed by the speed, I switched to int arrays and it was a HUGE difference. However I do agree that Edan should be aware of Java Collections and consciously decide that an array of ints is better. In this case he seems to simply be porting code from C/C++ as evident from the prefix operator and the row,col parameters.
0

@Eden

The code is not that perfect; why are you doing a i < row-1

check on first for loop? I guess you should have done i < row check instead

Also, you do not require to pass the sizes explicitly. You could use .length field on arrays to get their sizes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.