java—两个不同2d数组的总和

zlwx9yxi  于 2021-06-30  发布在  Java
关注(0)|答案(4)|浏览(353)

我需要帮助写一个方法,找出2个不同大小的二维数组的总和。

public static int[][] summary(int[][] tab1, int[][] tab2, int x) {
    int[][] finalTab = new int[4][5]; // I took sizes of bigger one
    if (x < 0) {
        for (int i = 0; i < finalTab.length - 1; i++) {
            for (int j = 0; j < finalTab[i].length - 1; j++) {
                finalTab[i][j] = tab1[i][j] + tab2[i][j];
                if (tab1[i][j] == 0) {
                    finalTab[i][j] = tab2[i][j];
                }
            }
        }
        for (int i = 0; i < finalTab.length; i++) {
            for (int j = 0; j < finalTab[i].length; j++) {
                System.out.print(" " + finalTab[i][j] + " ");
            }
            System.out.println();
        }
    }
    return finalTab;
}

输入为:

int [][] tab1 = {
        {2, 4, 6, 8},
        {2, 4, 6, 8},
        {2, 4, 6, 8},
};

int [][] tab2 = {
        {1, 3, 5, 7, 9},
        {1, 3, 5, 7, 9},
        {1, 3, 5, 7, 9},
        {1, 3, 5, 7, 9},
};

输出为:

3  7  11  15  0 
 3  7  11  15  0 
 3  7  11  15  0 
 0  0  0  0  0

输出应为:

3  7  11  15  9
 3  7  11  15  9
 3  7  11  15  9
 1  3  5  7  9

如何替换所有的 0 与tab2的索引对应的数字?

kmpatx3s

kmpatx3s1#

我是这样做的:

for (int i = 0; i < finalTab.length; i++)
    for (int j = 0; j < finalTab[i].length; j++)
        if (tab1.length > tab2.length) {
            finalTab[i][j] = tab1[i][j];
        } else
            finalTab[i][j] = tab2[i][j];

for (int i = 0; i < tab1.length; i++) {
    for (int j = 0; j < tab1[i].length; j++) {
        finalTab[i][j] = tab1[i][j] + tab2[i][j];
    }
}
uelo1irk

uelo1irk2#

你可以用 IntStream.of(int...) 方法来连接来自不同源的两个元素:

int[][] tab1 = {
        {2, 4, 6, 8},
        {2, 4, 6, 8},
        {2, 4, 6, 8}};
int[][] tab2 = {
        {1, 3, 5, 7, 9},
        {1, 3, 5, 7, 9},
        {1, 3, 5, 7, 9},
        {1, 3, 5, 7, 9}};
int[][] tab3 = IntStream
        // iterate over indices of tab2,
        // because it is biggest array
        .range(0, tab2.length).mapToObj(i ->
                IntStream.range(0, tab2[i].length).map(j ->
                        // for each cell concatenate element
                        // from tab2 with element from tab1
                        // if exists, or with 0 otherwise
                        IntStream.of(tab2[i][j],
                                i < tab1.length && j < tab1[i].length ?
                                        tab1[i][j] : 0)
                                // sum of two
                                // elements
                                .sum())
                        .toArray())
        .toArray(int[][]::new);
Arrays.stream(tab3).map(Arrays::toString).forEach(System.out::println);
// [3, 7, 11, 15, 9]
// [3, 7, 11, 15, 9]
// [3, 7, 11, 15, 9]
// [1, 3, 5, 7, 9]

另请参见:
•将二维数组中每列的所有元素相加
•仅使用一个分号在java中旋转int数组

mu0hgdu0

mu0hgdu03#

这是一个更直接的解决方案:

for (int i = 0; i < finalTab.length; i++) 
    for (int j = 0; j < tab1[i].length; j++) {
        int v1 = (i<tab1.length && j<tab1[i].length) ? tab1[i][j] : 0;
        int v2 = (i<tab2.length && j<tab2[i].length) ? tab2[i][j] : 0;
        finalTab[i][j] = v1 + v2;
    }
uz75evzq

uz75evzq4#

你可以做得更普通些

public static int[][] summary(int[][] tab1, int[][] tab2, int x) {
    int maxLenX = tab1.length > tab2.length ? tab1.length : tab2.length;
    int maxLenY = tab1[0].length > tab2[0].length ? tab1[0].length : tab2[0].length;
    int[][] finalTab = new int[maxLenX][maxLenY]; // i took sizes of bigger one
    if (x < 0) {
        for (int i = 0; i <= finalTab.length - 1; i++) {
            for (int j = 0; j <= finalTab[i].length - 1; j++) {
                if (i > tab1.length - 1 || j > tab1[i].length - 1) {
                    finalTab[i][j] = tab2[i][j];
                } else if (i > tab2.length - 1 || j > tab2[i].length - 1) {

                    finalTab[i][j] = tab1[i][j];
                } else {
                    finalTab[i][j] = tab1[i][j] + tab2[i][j];
                }
            }
        }
        for (int i = 0; i < finalTab.length; i++) {
            for (int j = 0; j < finalTab[i].length; j++) {
                System.out.print(" " + finalTab[i][j] + " ");
            }
            System.out.println();
        }
    }
    return finalTab;
}

所以你可以这样称呼它

summary(tab2, tab1, -1);

summary(tab1, tab2, -1);

相关问题