public class MatrixAddition {
public static void main(String[] args) {
int ar1[][] = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } };
int ar2[][] = { { 8, 7, 6 }, { 5, 4, 3 }, { 2, 1, 0 } };
addArray(ar1, ar2);
}
private static void addArray(int[][] tmp1, int[][] tmp2) {
int[][] sum = {};
System.out.println(" ");
System.out.println("The sum of the two matrices is");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum[i][j] = tmp1[i][j] + tmp2[i][j];
System.out.print(sum[i][j] + " ");
}
}
}
}
output:
The sum of the two matrices is
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
int[][] sum = {};does?