求二维数组中各列的最大值

时间:2023-01-31 21:29:47

I was wondering while using java.

在使用java的时候我在想。

There are two-dimensional arrays as shown below.

有二维数组,如下所示。

int[][] test = {{3, 9, 3, 5}, {4, 19, 4, 9}, {2, 10, 5, 6}};

I want to find the max value in each row in a two-dimensional array, and I wonder how to code it.

我想求二维数组中每一行的最大值,我想知道如何编码它。

I want to result is

我想要的结果是

int[] answer = {4, 19, 5, 9}

Simply add a comment, I want to extract the largest value.

只要添加一个注释,我就想提取最大的值。

[0][0] = 3;
[1][0] = 4;
[2][0] = 2;

[0][1] = 9;
[1][1] = 19;
[2][1] = 10;

[0][2] = 3;
[1][2] = 4;
[2][2] = 5;

[0][3] = 5;
[1][3] = 9;
[2][3] = 6;


So max value is 4, 19, 5, 9

I tried this method but this method is arr[i][j] compares all elements in a two-dimensional array and finds only max in the comparison.

我尝试了这个方法,但是这个方法是arr[I][j]比较二维数组中的所有元素,在比较中只找到max。

for (int i = 0; i < test.length; i++) {
    int max = test[i][0];
    for (int j = 0; j < test[i].length; j++)      
        if (test[i][j] > max)
            max = test[i][j]
    }

2 个解决方案

#1


3  

You can init the result with the first row, then iterate on this matrix, if the current element test[row][column] is greater than result[column], reassign result[column]:

您可以使用第一行来初始化结果,然后在这个矩阵上迭代,如果当前的元素测试[行][列]大于结果[列],再分配结果[列]:

public static void main(String[] args) {
    int[][] test = {{3, 9, 3, 5}, {4, 19, 4, 9}, {2, 10, 5, 6}};
    int[] result = test[0];
    for (int row = 1; row < test.length; row++) {
        for (int column = 0; column < test[0].length; column++) {
            if (test[row][column] > result[column]) {
                result[column] = test[row][column];
            }
        }
    }
    for (int number : result) {
        System.out.print(number + " "); // 4 19 5 9 
    }
}

#2


1  

According to your exemple, the "big" loop is going through the columns and the "small" loop through the lines. Sou you'll have something like that :

根据您的例子,“大”循环通过列,“小”循环通过行。你会有这样的东西:

for(int j=0; j<test[0].length; j++) { //loop through columns
    for(int i=0; i<test.length; i++) { //loop through lines
    }
}

Then you need a result variable that will be an array : int[] result = new int[test[0].length];

然后需要一个结果变量,它将是一个数组:int[] result = new int[test[0].length];

Then you need to have a variable to store the max number of a column. This variable will be re-initialise at every "big" loop since you want to re-determine the max.

然后需要一个变量来存储列的最大数目。这个变量将在每个“大”循环中重新初始化,因为您想重新确定最大值。

int[] result = new int[test[0].length]; //Array to store the result
for(int j=0; j<test[0].length; j++) { //loop through columns
    int max = 0; //int to store the max of the i column
    for(int i=0; i<test.length; i++) { //loop through lines
    }
}

Then for every line check if the number is bigger than max. If it is replace max by the new value.

然后对每一行检查这个数是否大于最大值。如果用新值替换最大值。

if(test[i][j] > max) { //if the number at the column i and line j is bigger than max
    max = test[i][j]; then max becomes this number
}

Finally when you've run through every line of the column, add the found max to the result

最后,当您遍历列的每一行时,将找到的最大值添加到结果中

result[i] = max; //Add the found max to the result array

This should give you this :

这应该会告诉你:

int[] result = new int[test[0].length]; //Array to store the result
for(int j=0; j<test[0].length; j++) { //loop through columns
    int max = 0; //int to store the max of the i column
    for(int i=0; i<test.length; i++) { //loop through lines
        if(test[i][j] > max) { //if the number at the column i and line j is bigger than max
            max = test[i][j]; then max becomes this number
        }
    }
    result[i] = max; //Add the found max to the result array
}
System.out.println(Arrays.toString(result)); //print the result

#1


3  

You can init the result with the first row, then iterate on this matrix, if the current element test[row][column] is greater than result[column], reassign result[column]:

您可以使用第一行来初始化结果,然后在这个矩阵上迭代,如果当前的元素测试[行][列]大于结果[列],再分配结果[列]:

public static void main(String[] args) {
    int[][] test = {{3, 9, 3, 5}, {4, 19, 4, 9}, {2, 10, 5, 6}};
    int[] result = test[0];
    for (int row = 1; row < test.length; row++) {
        for (int column = 0; column < test[0].length; column++) {
            if (test[row][column] > result[column]) {
                result[column] = test[row][column];
            }
        }
    }
    for (int number : result) {
        System.out.print(number + " "); // 4 19 5 9 
    }
}

#2


1  

According to your exemple, the "big" loop is going through the columns and the "small" loop through the lines. Sou you'll have something like that :

根据您的例子,“大”循环通过列,“小”循环通过行。你会有这样的东西:

for(int j=0; j<test[0].length; j++) { //loop through columns
    for(int i=0; i<test.length; i++) { //loop through lines
    }
}

Then you need a result variable that will be an array : int[] result = new int[test[0].length];

然后需要一个结果变量,它将是一个数组:int[] result = new int[test[0].length];

Then you need to have a variable to store the max number of a column. This variable will be re-initialise at every "big" loop since you want to re-determine the max.

然后需要一个变量来存储列的最大数目。这个变量将在每个“大”循环中重新初始化,因为您想重新确定最大值。

int[] result = new int[test[0].length]; //Array to store the result
for(int j=0; j<test[0].length; j++) { //loop through columns
    int max = 0; //int to store the max of the i column
    for(int i=0; i<test.length; i++) { //loop through lines
    }
}

Then for every line check if the number is bigger than max. If it is replace max by the new value.

然后对每一行检查这个数是否大于最大值。如果用新值替换最大值。

if(test[i][j] > max) { //if the number at the column i and line j is bigger than max
    max = test[i][j]; then max becomes this number
}

Finally when you've run through every line of the column, add the found max to the result

最后,当您遍历列的每一行时,将找到的最大值添加到结果中

result[i] = max; //Add the found max to the result array

This should give you this :

这应该会告诉你:

int[] result = new int[test[0].length]; //Array to store the result
for(int j=0; j<test[0].length; j++) { //loop through columns
    int max = 0; //int to store the max of the i column
    for(int i=0; i<test.length; i++) { //loop through lines
        if(test[i][j] > max) { //if the number at the column i and line j is bigger than max
            max = test[i][j]; then max becomes this number
        }
    }
    result[i] = max; //Add the found max to the result array
}
System.out.println(Arrays.toString(result)); //print the result