Home > Java > javaTutorial > Java Example - Get the maximum and minimum value of an array

Java Example - Get the maximum and minimum value of an array

黄舟
Release: 2017-02-18 10:08:18
Original
1587 people have browsed it

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

/*
 author by w3cschool.cc
 文件名:Main.java
 */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("最小值: " + min);
      System.out.println("最大值: " + max);
   }}
Copy after login

The above code runs The output result is:

最小值: 1
最大值: 9
Copy after login

The above is the Java example - the content of obtaining the maximum and minimum values ​​​​of the array. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template