2
public static int[][] copyMatrix(int[][] matrix) 
{
    for (int i = 0; (i < matrix.length); i++)
    {
        int[][] duplicateMatrix = new int[matrix.length][matrix[i].length];

            for (int j = 0; (j < matrix[i].length); j++)
            {
                duplicateMatrix[i][j] = matrix[i][j];
            }
    }
    return duplicateMatrix;
}

hello all, this specific function doesnt seem to work since duplicateMatrix isnt initialized as a variable, but I cant seem to initialize since its being created in the loop, I cant find a way to generate the amount of cells need in a column. help will be appreciated. thanks.

1
  • The parenthese here (j < matrix[i].length) can be omitted. Commented Dec 23, 2014 at 13:38

2 Answers 2

6

You should initialize the array before the loops, since you only want to initialize it once.

public static int[][] copyMatrix(int[][] matrix) 
{
    if (matrix.length < 1) {
        return new int[0][0];
    }
    int[][] duplicateMatrix = new int[matrix.length][matrix[0].length];
    for (int i = 0; (i < matrix.length); i++)
    {
            for (int j = 0; (j < matrix[i].length); j++)
            {
                duplicateMatrix[i][j] = matrix[i][j];
            }
    }
    return duplicateMatrix;
}

This code assumes that all the rows in your input array have the same number of elements (which is true for matrices).

You can relax this assumption if you remember that a 2-dimentional array is simply an array of arrays :

public static int[][] copyMatrix(int[][] matrix) 
{
    int[][] duplicateMatrix = new int[matrix.length][];
    for (int i = 0; (i < matrix.length); i++)
    {
            duplicateMatrix[i] = new int[matrix[i].length];
            for (int j = 0; (j < matrix[i].length); j++)
            {
                duplicateMatrix[i][j] = matrix[i][j];
            }
    }
    return duplicateMatrix;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Well, you don't need to assume that if you omit "row length" and set it in the first loop. But you're right, you don't need that special treatment in case of matrices.
I would suggest special-casing an empty matrix to avoid accessing index 1 when it doesn't exist.
@bcsb1001 You mean index 0?
1

A two-dimensional array is an array of arrays. You must first create the two-dimensional array, and then each one of its element individually:

public static int[][] copyMatrix(int[][] matrix) 
{
    int[][] duplicateMatrix = new int[matrix.length][];
    for (int i = 0; (i < matrix.length); i++)
    {
        duplicateMatrix[i] = new int[matrix[i].length];

        for (int j = 0; (j < matrix[i].length); j++)
        {
            duplicateMatrix[i][j] = matrix[i][j];
        }
    }
    return duplicateMatrix;
}

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.