자바 초보부터 B형까지 - 자바에서 정렬을 하는 기본적인 방법들 -

1. Arrays.sort()

 

import java.util.Arrays;

 

Arrays.sort(arr)은 배열 arr을 오름차순 정렬

 

import java.util.Arrays;

public class Main{
    public static void main(String[] args){
        int[] arr = new int[]{12,41,37,81,19,25,60,20};
        Arrays.sort(arr);
        
        for(int i = 0; i < 8; i++){
            System.out.print(arr[i] + " ");
            //12, 19, 20, 25, 37, 41, 60, 81
        }
    }
}

 

2. 부분정렬

 

특이하게 구간 인덱스를 명시하면 해당 구간만 정렬해줄 수도 있다

 

Arrays.sort(arr,시작 index, 끝 index+1) 형태로 작성하면, (시작 index~끝 index구간)만 오름차순 정렬

 

import java.util.Arrays;

public class Main {
    public static void main(String[] args){
        int[] arr = new int[]{12, 41, 37, 81, 19, 25, 60, 20};
        Arrays.sort(arr,2,4);

        for(int i = 0; i < 8; i++){
            System.out.print(arr[i] + " ");
        }
        // 12,41,37,81,19,25,60,20
    }
}

 

3. 내림차순 정렬

 

자바에서는 int[]로 선언된 배열은 한번에 내림차순 정렬할 수 있는 방법이 존재하지 않는다.

 

Integer[]로 선언된 배열이라면...

 

import java.util.Collections;

import java.util.Arrays;

 

Arrays.sort(arr, Collections.reverseOrder());로 내림차순 정렬 가능

 

import java.util.Arrays;
import java.util.Collections;

public class Main {
    public static void main(String[] args){
        Integer[] arr = new Integer[]{12,41,37,81,19,25,60,20};

        Arrays.sort(arr, Collections.reverseOrder());

        for(int i = 0; i < 8; i++){
            System.out.print(arr[i] + " ");
        }
        // 81,60,41,37,25,20,19,12
    }
}

 

stream을 이용해서 다음과 같이 int[] 배열을 Integer[] 배열로 바꿀 수 있다고 함

 

Integer[] arr2 = Arrays.stream(arr).boxed().toArray(Integer[]::new);

 

import java.util.Arrays;
import java.util.Collections;

public class Main {
    public static void main(String[] args){
        int[] arr = new int[]{12,41,37,81,19,25,60,20};

        Integer[] arr2 = Arrays.stream(arr).boxed().toArray(Integer[]::new);

        Arrays.sort(arr2, Collections.reverseOrder());

        for(int i = 0; i < 8; i++){
            System.out.print(arr2[i] + " ");
        }
        //81,60,41,37,25,20,19,12
    }
}

 

 

4. 문자열 정렬

 

문자열을 한번에 정렬하는 방법은 없다.

 

문자열을 먼저 char[] 배열로 만들기 위해 .toCharArray()로 바꿔주고

 

char[] 배열을 Arrays.sort()로 하면 알파벳 순으로 오름차순 정렬을 해준다.

 

그리고 new String(arr)로 해주면 char[] 배열을 다시 String으로 바꿔준다.

 

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        // 여기에 코드를 작성해주세요.

        String str = "badc";

        char[] chars = str.toCharArray();

        Arrays.sort(chars);

        String sortedStr = new String(chars);

        System.out.println(sortedStr);
        //abcd
    }
}

 

 

5. 문자열들의 배열 정렬

 

문자열을 원소로 가지는 String[]을 오름차순으로 정렬할려면, Arrays.sort()를 사용

 

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[] words = new String[]{"banana", "apple", "cat", "app"}; 
        Arrays.sort(words);

        for(int i = 0; i < 4; i++)
            System.out.println(words[i]); // "app", "apple", "banana", "cat"
    }
}

 

내림차순 정렬하고 싶다면, import java.util.Collections;를 수행하고 Collections.reverseOrder()를 인자로 넣어준다

 

import java.util.Arrays;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
        String[] words = new String[]{"banana", "apple", "cat", "app"}; 
        Arrays.sort(words, Collections.reverseOrder());

        for(int i = 0; i < 4; i++)
            System.out.println(words[i]); // "cat", "banana", "apple", "app"
    }
}
TAGS.

Comments