Arrays는 java.util소속 클래스로 배열과 관련된 여러 메서드들이 포함되어 있습니다.
! 반드시 import 해주어야 사용할 수 있습니다.
기본적으로 sort() 메서드는 오름차순 정렬을 해줍니다.
내림차순 정렬을 하고싶다면?
내림차순 정렬을 하기 위해서는 int가 아니라 Integer배열로 변환해준 뒤 Collections혹은 Comparator의 reaverseOreder() 메서드를 이용해 내림차순 정렬을 해야 합니다. 그냥 int배열에는 reverseOrder()가 적용되지 않습니다.
만약 int배열로 다시 변환하고 싶다면 mapToInt를 이용해 다시 한 번 변환해줄 수 있습니다.
import java.util.Arrays;
import java.util.Collections;
//import java.util.Comparator;
public class Main {
public static void main (String[] agrs) {
// 정렬할 배열
int[] arr = {8, 3, 5, 1, 7, 0};
// Integer배열 생성
Integer[] arr2 = Arrays.stream(arr).boxed().toArray(Integer[] :: new);
// 내림차순 정렬
Arrays.sort(arr2, Collections.reverseOrder());
// Arrays.sort(arr2, Comparator.reverseOrder()); 같은 결과
System.out.println(Arrays.toString(arr2));
// int배열 생성
int[] desc = Arrays.stream(arr2).mapToInt(i -> i).toArray();
System.out.println(Arrays.toString(desc));
}
}

+) 참고
How to convert int[] to Integer[] in Java?
I'm new to Java and very confused. I have a large dataset of length 4 int[] and I want to count the number of times that each particular combination of 4 integers occurs. This is very similar to
stackoverflow.com
How to convert Integer[] to int[] array in Java?
Is there a fancy way to cast an Integer array to an int array? (I don't want to iterate over each element; I'm looking for an elegant and quick way to write it) The other way around I'm using
stackoverflow.com
+) Arrays클래스 더 알아보기
코딩교육 티씨피스쿨
4차산업혁명, 코딩교육, 소프트웨어교육, 코딩기초, SW코딩, 기초코딩부터 자바 파이썬 등
tcpschool.com
Arrays class in Java - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
'STUDY > Java' 카테고리의 다른 글
Java | 추상클래스(abstract class) / 인터페이스(Interface) (0) | 2020.05.18 |
---|---|
Java | 접근 제어자/접근 제한자 ( Access Modifier ) (0) | 2020.05.14 |
Java | 객체 지향 프로그래밍(OOP : Object Oriented Programming)의 주요 개념 (0) | 2020.05.14 |
Java | 컬렉션 프레임워크 Collection Framework ( List / Set / Map ) (0) | 2020.05.12 |
Java | 자바 가상 머신 JVM(Java Virtual Machine) (0) | 2020.04.22 |