Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find the Largest number in the first column of Java Array

I have an array

int [ ][ ] matrix = {{ 10, 23, 93, 44 },{ 22, 34, 25, 3 },{ 84, 11, 7, 52 }};

and I want to find the max value on the first column of this array which is 84

This is my whole code which give me the max value of each column in this array

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

     public class Sample {  
  public static void main(String[] args) {

int[][] matrix = {{10, 23, 93, 44}, {22, 34, 25, 3}, {84, 11, 7, 52}};

int[] max = matrix[0];

for (int row = 1; row < matrix.length; row++) {
    
    for (int column = 0; column < matrix[0].length; column++) {
        
        if (matrix[row][column] > max[column]) {
            
            max[column] = matrix[row][column];
        }
    }
}
for (int number : max) {
    System.out.print(number + " ");
    }
}}

>Solution :

public static void main(String[] args) {
  int[][] matrix = {{10, 23, 93, 44}, {22, 34, 25, 3}, {84, 11, 7, 52}};

  int max = matrix[0][0];
  for (int row = 1; row < matrix.length; row++) {
    if (max < matrix[row][0]) {
      max = matrix[row][0];
    }
  }
  System.out.println(max);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading