英文:
How to sum the elements of two 2d arrays?
问题
我正尝试使用Java流API将两个二维数组的元素相加。我已经成功解决了一个一维数组的问题,但是我不知道如何进一步处理二维数组。
以下是将其转换的代码:
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;
}
而这是我为了相同目的而仅针对一维数组编写的代码:
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;
}
特别地,我不知道如何在二维数组上使用map()
方法。
英文:
I'm trying to add the elements of two two-dimensional arrays with each other, by using the java stream API.<br>
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.
答案1
得分: 6
这可以使用IntStream
及其方法mapToObj
来处理行,以及map
来处理每行中的元素:
static int[][] add(int[][] a, int [][] b) {
return IntStream.range(0, a.length)
.mapToObj(i -> add(a[i], b[i])) // int[] is object
.toArray(int[][]::new); // create new 2D array
}
static int[] add(int[] a, int[] b) {
return IntStream.range(0, a.length)
.map(i -> a[i] + b[i]) // processing int operands
.toArray(); // IntStream.toArray() returns int[]
}
测试部分:
int[][] a = {
{1, 2},
{3, 4}
};
int[][] b = {
{10, 20},
{30, 40}
};
System.out.println("Sum of a + b = ");
Arrays.stream(add(a, b))
.map(Arrays::toString)
.forEach(System.out::println);
输出部分:
Sum of a + b =
[11, 22]
[33, 44]
单方法的实现可以如下:
static int[][] add2D(int[][] a, int [][] b) {
return IntStream
.range(0, a.length)
.mapToObj(i -> IntStream
.range(0, a[i].length)
.map(j -> a[i][j] + b[i][j])
.toArray()
)
.toArray(int[][]::new);
}
英文:
This can be implemented using IntStream
and its methods mapToObj
to handle rows and map
to handle elements in each row:
static int[][] add(int[][] a, int [][] b) {
return IntStream.range(0, a.length)
.mapToObj(i -> add(a[i], b[i])) // int[] is object
.toArray(int[][]::new); // create new 2D array
}
static int[] add(int[] a, int[] b) {
return IntStream.range(0, a.length)
.map(i -> a[i] + b[i]) // processing int operands
.toArray(); // IntStream.toArray() returns int[]
}
Test
int[][] a = {
{1, 2},
{3, 4}
};
int[][] b = {
{10, 20},
{30, 40}
};
System.out.println("Sum of a + b = ");
Arrays.stream(add(a, b))
.map(Arrays::toString)
.forEach(System.out::println);
Output
Sum of a + b =
[11, 22]
[33, 44]
Single-method implementation may be as follows:
static int[][] add2D(int[][] a, int [][] b) {
return IntStream
.range(0, a.length)
.mapToObj(i -> IntStream
.range(0, a[i].length)
.map(j -> a[i][j] + b[i][j])
.toArray()
)
.toArray(int[][]::new);
}
答案2
得分: 0
你可以使用 Stream#reduce
方法来对两个或更多个二维数组的元素进行求和:
public static int[][] sumArrays(int[][]... arrays) {
// 将数组流逐个合并为单个数组
return Arrays.stream(arrays).reduce((a1, a2) -> IntStream
// 遍历最大数组行的索引
.range(0, Math.max(a1.length, a2.length))
.mapToObj(i -> IntStream
// 遍历最大行的单元格索引
.range(0, Math.max(
i < a1.length ? a1[i].length : 0,
i < a2.length ? a2[i].length : 0))
// 对两行元素求和,如果存在则相加,否则为0
.map(j -> (i < a1.length && j < a1[i].length ? a1[i][j] : 0)
+ (i < a2.length && j < a2[i].length ? a2[i][j] : 0))
.toArray())
.toArray(int[][]::new))
.orElse(null);
}
// 测试
public static void main(String[] args) {
int[][] arr1 = {
{1},
{1, 1}};
int[][] arr2 = {
{2},
{2, 2},
{2, 2, 2}};
int[][] arr3 = {
{3, 3, 3, 3},
{3, 3, 3, 3},
{3, 3, 3, 3},
{3, 3, 3, 3}};
int[][] sum = sumArrays(arr1, arr2, arr3);
// 输出
Arrays.stream(sum).map(Arrays::toString).forEach(System.out::println);
//[6, 3, 3, 3]
//[6, 6, 3, 3]
//[5, 5, 5, 3]
//[3, 3, 3, 3]
}
<sup>另请参阅:两个不同二维数组的求和</sup>
英文:
You can use Stream#reduce
method to sum the elements of two or more 2d arrays:
public static int[][] sumArrays(int[][]... arrays) {
// reduce the stream of arrays into a single
// array by sequentially summing array pairs
return Arrays.stream(arrays).reduce((a1, a2) -> IntStream
// iterate over the indices of
// the rows of the largest array
.range(0, Math.max(a1.length, a2.length))
.mapToObj(i -> IntStream
// iterate over the indices of
// the cells of the largest row
.range(0, Math.max(
i < a1.length ? a1[i].length : 0,
i < a2.length ? a2[i].length : 0))
// sum the elements of two rows if exist, or 0 otherwise
.map(j -> (i < a1.length && j < a1[i].length ? a1[i][j] : 0)
+ (i < a2.length && j < a2[i].length ? a2[i][j] : 0))
.toArray())
.toArray(int[][]::new))
.orElse(null);
}
// test
public static void main(String[] args) {
int[][] arr1 = {
{1},
{1, 1}};
int[][] arr2 = {
{2},
{2, 2},
{2, 2, 2}};
int[][] arr3 = {
{3, 3, 3, 3},
{3, 3, 3, 3},
{3, 3, 3, 3},
{3, 3, 3, 3}};
int[][] sum = sumArrays(arr1, arr2, arr3);
// output
Arrays.stream(sum).map(Arrays::toString).forEach(System.out::println);
//[6, 3, 3, 3]
//[6, 6, 3, 3]
//[5, 5, 5, 3]
//[3, 3, 3, 3]
}
<sup>See also: Sum of 2 different 2d arrays</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论