Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Ints max() function in Java



The max() method of the Ints class returns the greatest value present in array. Following is the syntax −

static int max(int... array)

Let us first see an example −

Example

import com.google.common.primitives.Ints;
class Demo {
   public static void main(String[] args) {
      int[] myArr = { 10, 20, 30, 40, 50, 60,20, 80, 20, 100 };
      System.out.println(Ints.join("-", myArr));
      System.out.println("Maximum value from the array = " + Ints.max(myArr));
      // finding last index of element 20
      int index = Ints.lastIndexOf(myArr, 20);
      if (index != -1) {
         System.out.println("The last index of element 20 = " + index);
      } else {
         System.out.println("The element 20 is not in the array.");
      }
   }
}

Output

Maximum value from the array = 100
10-20-30-40-50-60-20-80-20-100
The last index of element 20 = 8
Updated on: 2019-09-24T07:39:45+05:30

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements