Latest web development tutorials

Javaインスタンス - 最大値と最小値の配列

Javaの例 Javaの例

次の例では、Collection.maxコレクションクラス()とCollection.min()メソッドは、配列内の最大値と最小値を検索する方法を示しています。

/*
 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);
   }
}

上記のコードは出力され実行されます。

最小值: 1
最大值: 9

Javaの例 Javaの例