Java-在二维数组中查找最大数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32035161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:32:27  来源:igfitidea点击:

Java- Finding maximum number in a 2D array

javaarraysloops

提问by Tia

This program compares two numbers in a 2*5 array which the user inputs and displays the largest number between the two. But I have some problem in understanding what [0]really does in int max = array[i][0]. I mean, shouldn't it be int max = array[i][j]? Because basically, what I understand from array[i][0]is that the numbers being input for the rows is being compared with the first element of the column since it's [0]but I would like some explanation how it actually works.

该程序比较用户输入的 2*5 数组中的两个数字,并显示两者之间的最大数字。但我理解一些什么问题[0]确实在int max = array[i][0]。我的意思是,不应该int max = array[i][j]吗?因为基本上,我的理解array[i][0]是将输入的行数与列的第一个元素进行比较,因为它是,[0]但我想解释一下它的实际工作原理。

import java.util.*;

public class L2 {

    public static void main(String[] args) {

        Scanner input= new Scanner(System.in);

        System.out.println("Please enter 10 integers");

        int array[][]= new int[2][5];

        for(int i=0; i<array.length; i++){

            int max= array[i][0];
            for(int j=0; j<array[i].length; j++){

                array[i][j]= input.nextInt();

                if(max<array[i][j]){

                    max= array[i][j];
                }
            }
            System.out.println("Maximum number in the row is "+ max);
            System.out.println();
        }              

    }

}

回答by hugh

It's taking a 2*5 array of integers and finding the maximum in each row of 5, (then presumably summing these (2) values up, although that code isn't included in your snipper).

它需要一个 2*5 的整数数组,并在每行 5 中找到最大值(然后大概将这些 (2) 值相加,尽管该代码未包含在您的 snipper 中)。

The line

线

int max= array[i][0];

is initializing the maximum value on the ith row to the first value in that row, than scanning the row to find any larger and increase maxaccordingly. Typically you'd then start the loop counter from 1, since the 0th value is already dealt with:

正在将i第 th 行上的最大值初始化为该行中的第一个值,而不是扫描该行以找到更大的值并相应地增加max。通常你会从 1 开始循环计数器,因为已经处理了第 0 个值:

for(int j=1; j<array[i].length; j++){

Another common approach is to initialize maxto MIN_INTinstead, since all values will be greater than this.

另一种常见的方法是初始化maxMIN_INT,因为所有值都将大于此值。

回答by someoneGreat

//I hope this helps you 
    public class Compare{


        public static void main(String[] args) {

            Scanner input= new Scanner(System.in);
int row =2;
int col = 5;
         int array[][]= new int[row][col];
            System.out.println("Please enter 10 integers");
            for(int i=0; i<row; i++){
            for(int j=0; j<col;j++){
                    array[i][j]= input.nextInt();
}
}
int max = Integer.MIN_VALUE;
      for(int i=0; i<row; i++){
          for(int j =0; j<col; j++){
                    if(max<array[i][j]){
                        max = array[i][j];
                    }
          }
                System.out.println("Maximum number in the row is "+ max);
                System.out.println();
            }              

        }

            }

回答by Ritam Chakraborty

In other way you can use Collection.max(Stream.of(..).collect(Collectors.toList()));

以其他方式您可以使用 Collection.max(Stream.of(..).collect(Collectors.toList()));

Here's an example

这是一个例子

System.out.println(Collections.max(Stream.of(new int[][]{{1, 2}, {3, 4}})
        .map(a -> Collections.max(Arrays.stream(a).boxed()
            .collect(Collectors.toList())))
            .collect(Collectors.toList())));
Output: 4

回答by Amit Bhati

In below line, the max is getting initialized with the first element of the row:-

在下面的行中,最大值正在使用该行的第一个元素进行初始化:-

int max= array[i][0];

Your purpose is to find the maximum of each row, so basically you are starting the every iteration with the first element of the row.

您的目的是找到每一行的最大值,因此基本上您是从该行的第一个元素开始每次迭代。