3

I'm trying to do a few things with multidimensional arrays. I'm new to Java and not an excellent programmer, and I couldn't find anything else on the internet about this topic, so I thought I'd ask here.

Essentially I'm making a method that will add the values of two multidimensional integer arrays together to create a third multidimensional array. This is identical to matrix addition in the cases where the multidimensional arrays ARE matrices (e.g. two 2x3 arrays added together), but not so if I have a multidimensional array with variable row lengths. So far the method I have is this:

public static int[][] addMDArray(int[][] a, int[][] b)
{
    boolean columnequals = true;
    for (int row = 0; row < a.length; row++)
    {
        if (a[row].length != b[row].length)
        {
            columnequals = false;
        }
    }
    if (columnequals == false || a.length != b.length)
        System.out.println("The arrays must have the same dimensions!");
    else
    {
        int[][] sum = new int[a.length][a[0].length];
        for (int row = 0; row < a.length; row++)
        {
            for (int column = 0; column < a[row].length; column++)  
                sum[row][column] = a[row][column] + b[row][column];
        }
        return sum;
    }
    return null;
}

As I said, this works with MD arrays without variable row lengths; however, with the exception of the first part that checks that they have the same dimensions, this method will not work with two arrays like this:

int[][] g = {{2, 1}, {3, 5, 4}, {5, 7, 7}};
int[][] d = {{1, 2}, {3, 4, 5}, {5, 6, 7}};

The issue I'm running into is that I can't declare the "sum" MD array without specifying dimensions... Is there a way to create the sum array in the for loop itself? I feel like this would be the simplest solution (if possible), but otherwise I don't know what else to try.

Any help would be appreciated!

2
  • Hey, unrelated to your question, but will probably assist you later. You can use System.arraycopy() After creating a new matrix with the calculated height and width. Check this code out that I wrote to alter the size of a matrix: MatrixUtils.java Commented Nov 1, 2014 at 21:16
  • why not traverse array vertically and find max(horizontal-size) and then have the sum, have this max-dimension. Make sense? Commented Nov 1, 2014 at 21:26

1 Answer 1

2

You can: int[][] sum = new int[a.length][]; is perfectly legal. Then you can do sum[i] = new int[a[i].length];, and your code will look like this:

    int[][] sum = new int[a.length][];
    for (int row = 0; row < a.length; row++)
    {
        sum[row] = new int[a[row].length];
        for (int column = 0; column < a[row].length; column++)  
            sum[row][column] = a[row][column] + b[row][column];
    }

Just remember that a multi-dimensional array in Java is really an array of arrays. (Which isn't necessarily how things should be but it is what it is.)

(As a complete aside, you should probably throw an IllegalArgumentException when the two arrays don't have the same dimensions rather than return null. Your code will be a lot easier to use that way.)

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

2 Comments

Thank you so much! I wasn't aware that you could declare a multidimensional array in which the length of the arrays within the array aren't established--that completely solves this problem. I would agree with you that returning null seems pretty useless. However, this is part of a lab for a Java course I'm taking, and we haven't covered exception handling yet, so this is the way the professor wants it done. I should clarify that I wasn't just looking for someone to write my own code for me. This knowledge is truly useful and will allow me to do other parts of this lab, so thank you!
@ChristianSilva That explains it then. Don't worry about the code, you clearly did everything else and this was the only piece of the jigsaw missing.

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.