Java Examples - Get Maximum and Minimum Value from an Array

Java 实例Java Examples

The following example demonstrates how to find the maximum and minimum values in an array using the Collections.max() and Collections.min() methods of the Collections class:

Main.java file

import java.util.Arrays; import java.util.Collections; public class Main { public static void main(String[] args) { Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5}; int min = (int) Collections.min(Arrays.asList(numbers)); int max = (int) Collections.max(Arrays.asList(numbers)); System.out.println("Minimum value:" + min); System.out.println("Maximum value:" + max); } }

The output of running the above code is:

最小值: 1
最大值: 9

Java 实例Java Examples

Other Extensions