Sum of 2 different 2d arrays

筅森魡賤 提交于 2020-12-27 03:04:14

问题


I need help writing a method to find out a sum of 2 different sized 2d arrays.

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;
}

Input is:

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},
};

Output is:

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

Output should be:

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

How can I replace all of 0 with numbers corresponding to the index of tab2?


回答1:


you can do it more generic

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;
}

so you can call it wether like that

summary(tab2, tab1, -1);

or

summary(tab1, tab2, -1);



回答2:


This is a more straight-forward solution:

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;
    }



回答3:


I made it this way:

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];
    }
}



回答4:


You can use IntStream.of(int...) method to concatenate two elements from different sources:

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]

See also:
• Adding up all the elements of each column in a 2d array
• Rotating an int Array in Java using only one semicolon



来源:https://stackoverflow.com/questions/65373009/sum-of-2-different-2d-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!