Java Collection How to - Find the max and min of an an array of integer








Question

We would like to know how to find the max and min of an an array of integer.

Answer

import java.util.Arrays;
//  ww  w  .j av  a 2 s . co  m
public class Main {

  public static void main(String[] args) {
    int a[] = new int[]{1,2,4,1,5,7,3,4};
    Arrays.sort(a);
    int min = a[0];
    System.out.println("The minmum value is    " + min);
    int max = a[a.length - 1];
    System.out.println("The maxemum value is   " + max);

    for (int i = 0; i < a.length; i++) {
      System.out.println(a[i]);
    }
  }
}

The code above generates the following result.