[Java] The method sort(int[]) in the type Arrays is not applicable for the arguments (int[], Collections.reverseOrder())
int[] 배열에 대하여 내림차순 정렬을 하려고 시도하면 나오는 에러이다.
The method sort(int[]) in the type Arrays is not applicable for the arguments (int[], Collections.reverseOrder())
에러의 내용은 int[] 배열에서 Collections 메서드를 사용하지 못한다는 내용이다.
int는 자료형(primitive type)이라서 사용이 불가능 한것이다.
import java.util.*
public class Sort {
public static void main(String[] args) {
int[] array = {10, 8, 5, 15, 1, 33, 22};
Integer[] arr = Arrays.stream(array).boxed().toArray(Integer[]::new);
Arrays.sort(arr, Collections.reverseOrder());
System.out.println(Arrays.toString(arr));
}
}
int[] 대신 Integer[] 로 정렬을 해보면 아래 결과처럼 내림차순 정렬이 잘 동작하는 걸 확인할 수 있다.
[Java] java.util.ConcurrentModificationException: null (0) | 2023.03.30 |
---|
댓글 영역