https://www.acmicpc.net/problem/10818
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> numList = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
numList.add(sc.nextInt());
}
int max = Collections.max(numList);
int min = Collections.min(numList);
System.out.print(min + " ");
System.out.println(max);
}
}
|
cs |
이번 문제에서는 크게 제네릭(Generics)과 컬렉션(Collections)에 대해 알게 되었다.
먼저 제네릭이란 ArrayList나 List 등이 사용할 객체의 타입을 지정해주는 역할을 하는것으로 <> 안에 사용할 타입을 넣어주면 된다. 이는 다룰 객채의 타입을 미리 명시하여 객체의 형변환을 사용할 필요없게 하기 위함이고 또는 내가 사용하고 싶은 데이터 타입만 사용할 수 있게 하기 위함이다.
다음으로는 컬렉션이다.
컬렉션은 정적 유틸리티 메소드의 모음이 있는 클래스이다. 이 문제에서는 최대 최소를 구하기 위해 max와 min 메소드를 사용했다.
'코테' 카테고리의 다른 글
[백준 / Java] 4344번 : 평균은 넘겠지 (0) | 2023.02.06 |
---|---|
[백준 / Java] 8958번 : OX퀴즈 (0) | 2023.02.06 |
[백준 / Python] 2108번 : 통계학 (1) | 2023.02.03 |
[백준 / Python] 9020번 : 골드바흐의 추측 (0) | 2023.02.02 |
[백준 / Python] 2563번 : 색종이 (0) | 2023.02.01 |