Old Posts/Java

여러개의 숫자 중 Max,Min 구하기

bactoria 2018. 1. 7. 17:55

두 수중 큰수를 구하려면

Math.max(a,b) 를 사용하면 된다.


만약 비교해야 할 수가 여러개라면?


Math.max(a,b,c)는 사용할 수 없다. 

그렇다고 Math.max(Math.max(a,b),c) ... 이렇게 할수는 있겠지만 비교할 수가 더 크다면 이것도 적합한 방법은 아닐 것이다.


1.  int i[] = { 124, 634, 7, 5021525, 77, 8765, -356, 0 };

    Arrays.sort(i); 


2. LinkedList<Integer> list1 = new LinkedList<Integer>();

    list1.add(3);

    list1.add(5);

    list1.add(11);

    list1.add(2);

    list1.add(7);

    Integer i = Collections.max(list1);

(물론 ArrayList도 가능함)

상황에따라 알맞게 쓰면 될듯