Finding minimum and maximum values in a java array

In this post we are going to finding minimum and maximum values in a java array.

finding minimum and maximum values in a java array

Also read – convert decimal to octal java program

In finding minimum and maximum values in an array java we create two methods,

import java.util.*;

public class MinMaxDemo 
{
   public static void main(String[] args) 
   {
      int arrNum[] = new int[]{59, 26, 53, 9, 81, 238};
      // calling maxValue() method
      int maxNum = maxValue(arrNum);
      System.out.println("Maximum : " + maxNum);
      // calling minValue() method
      int minNum = minValue(arrNum);
      System.out.println("Minimum : " + minNum);
   }
	
   public static int maxValue(int[] arrInput)
   { 
      int max = arrInput[0];
      for(int a = 1; a < arrInput.length; a++)
      { 
         if(arrInput[a] > max)
         {
            max = arrInput[a];
         }
      }
      return max;
   }
	
   public static int minValue(int[] arrInput)
   {
      int min = arrInput[0];
      for(int a = 1; a < arrInput.length; a++)
      {
         if(arrInput[a] < min)
         {
            min = arrInput[a];
         }
      }
      return min;
   }
}

Output:

Maximum : 238
Minimum : 9