When the column count and row count are the same, this code works fine. When they differ it throws an exception inside the columnSum
method.
Here's the error message.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
And here's my code
public static void columnSum(int[][] anArray)
{
int sum = 0;
for (int col = 0; col < anArray.length; col++)
{
for (int r = 0; r < anArray[col].length; r++)
{
sum += anArray[r][col];
}
System.out.println("Sum of column " + col + " = " + sum);
}
System.out.println();
}
I have no idea why my code doesn't work when I have more columns than rows or vice versa.
Does anyone have idea?