Arrays.sort()

jayP·2022년 11월 12일
0
import java.util.Scanner;
import java.util.Arrays;

public class Main {
    
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int[] arr = new int[n];
        
        for (int i = 0; i < n; i++) {
            
            arr[i] = sc.nextInt();
            
        }
        Arrays.sort(arr);
        sc.close();
        System.out.println(arr[0] + " " + arr[n-1]);
       
    }
}

배열을 오름차순으로 정렬하고 싶다면 Arrays.sort() 메서드를 통하여 정렬 할 수 있습니다.

위 문제는 배열의 최대, 최소 값을 구하기 위하여 정렬을 진행하였고
배열 맨 앞, 맨 뒤에 최댓, 최솟값이 존재하도록하여 문제를 해결하는 예시입니다.

profile
Software Engineer

0개의 댓글