0

I have this method which takes two instances of two dimensional arrays to add them together and store the sum in a new array, the two arrays must have the same size eg.(the same rows and columns number) if not it should throw an exception that I defined. my method is throwing the exception only if the first array has a different number of rows and not columns for example the exception is thrown only when I pass these arrays: a[4][4] b[5][4] but not these arrays: a[4][5] b[4][5], can someone explain whats happing? and I'm I throwing the exception the right way?

public int[][] add(int[][] a, int[][] b) throws IncompatibleArgumentsException {
    int[][] sum = new int[a.length][b.length];

    if (a.length == b.length) {
        System.out.println("The Sum of the arrays is: ");
        System.out.println("  --------------- ");
        for (int row = 0; row < a.length; row++) {
            for (int col = 0; col < b.length; col++) {
                sum[row][col] = a[row][col] + b[row][col];

                System.out.println(" | " + a[row][col] + "  +  " + b[row][col] + "  =  " + sum[row][col] + " | ");
                System.out.println("  --------------- ");
            }
        }
    } else {
        throw new IncompatibleArgumentsException("Arrays have different size");
    }

    return sum;
}

and this is how am calling the method:

public Implementation() {

    int[][] x = new int[1][1];
    x[0][0] = 1;

    int[][] y = new int[1][2];
    y[0][0] = 1;
    y[0][1] = 3;

    add(x, y);
}
4
  • Well, you only throw an exception if a.length != a[0].length. So you're not even comparing the size of the two outer arrays. Only if the number of rows of the first array is equal to the number of columns of its first row. You need to compare the number of rows of the two arrays, and the number of elements of all the rows of the two arrays. I also don't understand why the result array is initialized as new int[a.length][b.length]. Commented Feb 17, 2017 at 21:10
  • @controlaltdel but its returning the sum, here is the console result:The Sum of the arrays is: --------------- | 1 + 1 = 2 | --------------- Commented Feb 17, 2017 at 21:10
  • @Khaled I apologize. I was thinking in my mind "2nd case should not cause an exception..." but it's not what I wrote. In any case it appears to match with what you've found Commented Feb 17, 2017 at 21:16
  • @controlAltDel please note that I have updated my if statement. Commented Feb 17, 2017 at 21:25

2 Answers 2

1

You have the wrong check. What you should be doing right at the top :

if (a.length != b.length || a[0].length != b[0].length) {
  throw new IncompatibleArgumentsException(...);
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you sir that did the trick. One more question can you advice how can I make the arrays that I pass more dynamic for example pass a number of columns and rows and set the values of them using a for loop or any better approach?
@Khaled, you can post a new question
0

'a.length' gives you the number of rows as essentially a 2-d array is an array of 1-d arrays, so a.'length' will simply give you the number of 1-d arrays in 'a'. To match the number of columns, you need to find out how many elements are there in the individual 1-d arrays of a, hence a[0].length (where a[0] = the 0th 1-d array in a) will give you the number of columns.

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.