Java Coding 編程系列: 初級篇之三 Print max and min in array 方便方法 用Java Collections.min Collections.max

Java Coding 編程系列: 初級篇之三 Print max and min in array 方便方法 用Java Collections.min Collections.max

 小薯仔在網上看到一個電腦高手快捷方便求Print max and min in array 方法: 


import java.util.Arrays;

import java.util.Collections;


public class getMinNMax {

  public static void main(String[] args) {


    Integer[] num = {   2,   11,  55, 99 };


    int min = Collections.min(Arrays.asList(num));

    int max = Collections.max(Arrays.asList(num));


    System.out.println("Minimum number of array is : " + min);

    System.out.println("Maximum number of array is : " + max);

  }

}


Reference: java - How to use Math.min and Math.max in integer array - Stack Overflow

把Array 用Collection 方法: 

1. Collections.min 找 collection 的min 

2. Collections.max 找 collection 的max

3. java.util.Arrays的asList()方法用於返回由指定數組支持的的Array

用這些方法可快捷方便求出 max and min in array


這個reference 太好了,忍不住拿來引用一下。





留言

這個網誌中的熱門文章

如何設定Google Form顯示語言 Display language 是某一種特定語言Particular Language ?

壞機救資料神器Hiren’s BootCD PE (免費軟件) - Boot Failure ? Scan Virus 先

Adobe Ai 外框outline 是什麼?轉了外框要再更改,後悔怎麼辦?