I'm trying to add the elements of two two-dimensional arrays with each other, by using the java stream API.
I managed the problem with a one-dimensional array, but I don't know how to proceed further with a two-dimensional array.
Here is the code to transform:
public static int[][] add(final int[][] m1, final int[][] m2) {
int[][] e = new int[m2.length][m2[0].length];
for (int i = 0; i < m1.length; i++) {
for (int j = 0; j < m1[i].length; j++) {
e[i][j] = m1[i][j] + m2[i][j];
}
}
return e;
}
And this is the code which I wrote for the same purpose but only with a one-dimensional array:
public static int[] addOneDimension(final int[] a, final int b[]) {
int[] c = IntStream.range(0, a.length)
.map(i -> a[i] + b[i])
.toArray();
return c;
}
In particular, I don't know how to use the map()
method on two-dimensional arrays.