2

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

    }

}

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

No, it would loop through the columns only 4 times if I start the loop counter for the inner loop from 1. It should start as from 0 only.
It would, but this would give the correct answer - max starts from the value of the first element, then this is compared with each of the remaining (4) values. It's not necessary to compare this value with the zeroth element, since it was already set equal to it, so you save one loop iteration. (Of course, the extra iteration won't change the result, so if you find this way more readable you might prefer to leave it in.)
2
//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);
            }

        }

    }

Comments

1

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.

Comments

0

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

Comments

0

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) + ", ");

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.