728x90

오름차순 정렬하기
Collections의 sort 메서드를 사용하자.
[JAVA] 컬렉션 프레임워크 _ 반복자(Iterator), 정렬
반복자(Iterator) : 컬렉션의 요소를 순회하면서 하나씩 추출하는데 사용한다. 때문에 컬렉션을 위해 존재하는 인터페이스라고 할 수 있다. 반복자의 메서드 hasNext() : 다음 순회할 데이터의 유무
studywithjw.tistory.com
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> list = new ArrayList<>();
for(int i = 0; i<n; i++) {
int num = sc.nextInt();
list.add(num);
}
Collections.sort(list);
for(int i = 0; i<list.size(); i++) {
System.out.print(list.get(i)+"\n");
}
}
}
728x90
'JAVA > 오류고민' 카테고리의 다른 글
[백준] 10807번 개수 세기 (0) | 2023.11.06 |
---|---|
[백준] 10178번 할로윈의 사탕 (1) | 2023.11.06 |
[백준] 10162번 전자레인지 (0) | 2023.11.06 |
[백준] 2908번 상수 (0) | 2023.10.23 |
[백준] 2490번 윷놀이 (0) | 2023.10.23 |