Tia
Tia

Reputation: 1230

Java- Finding maximum number in a 2D array

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.

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

    }

}

Upvotes: 2

Views: 27334

Answers (5)

someoneGreat
someoneGreat

Reputation: 19

//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();
                }
            }

            System.out.println(Arrays.deepToString(array));
            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);
            }

        }

    }

Upvotes: 2

Ender
Ender

Reputation: 122

The following code allows you to enter number of columns and rows and numbers. It outputs the array of numbers and highest numbers in each column. It expands your code a bit

public static void main(String[] args) {
    System.out.print("Enter number of rows: ");
    Scanner sc1 = new Scanner(System.in);
    int row = sc1.nextInt();
    
    System.out.print("Enter number of columns: ");
    Scanner sc2 = new Scanner(System.in);
    int col = sc2.nextInt();


    System.out.print("Enter numbers: ");
    Scanner sc = new Scanner(System.in);


    int array[][] = new int[row][col];

    for(int i = 0; i < row; i++) {
        for(int j = 0; j < col; j++) {
            array[i][j] = sc.nextInt();
        }
    }

    System.out.println(Arrays.deepToString(array));

    int max = Integer.MIN_VALUE;
    int[] arrayMax;
    System.out.print("Maximum numbers are: ");
    for(int i = 0; i < row; i++){
        for(int j = 0; j < col; j++) {
            if(max < array[i][j]) max = array[i][j];
        }
        arrayMax = new int[]{max};
        System.out.print(Arrays.toString(arrayMax) + ", ");

    }

}

Upvotes: 0

Ritam Chakraborty
Ritam Chakraborty

Reputation: 495

In other way you can use 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

Upvotes: 0

Amit Bhati
Amit Bhati

Reputation: 5649

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.

Upvotes: 1

hugh
hugh

Reputation: 2280

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).

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 max accordingly. Typically you'd then start the loop counter from 1, since the 0th value is already dealt with:

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

Another common approach is to initialize max to MIN_INT instead, since all values will be greater than this.

Upvotes: 2

Related Questions