본문 바로가기
JAVA

[JAVA] - List의 요소 위치 바꾸기 (swap)

by nam_ji 2024. 7. 16.

List의 요소 위치 바꾸기 (swap)

  • Collections.swap 작성 방법 
    • Collections.swap(List, 인덱스1, 인덱스2);
      선택한 List의 인덱스1 요소와 인덱스2 요소의 위치 변경

1. List를 Collections.swap() 메서드로 List 내 요소 위치 변경하기

  • 정수 List를 만들고
  • swap 메서드에 List와 바꿀 인덱스 번호 두개를 작성합니다.
  • 밑 예시에서 원래 List는 [1,2,3,4,5]를 출력하는게 맞지만 swap 메서드로 인해 [1,2,5,4,3]을 출력하게 됩니다.
package collectionsExample;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SwapMethod {
  public static void main(String[] args) {
    CollectionsSwap1 collectionsSwap = new CollectionsSwap1();
    collectionsSwap.collectionSwap();
  }
}

class CollectionsSwap1 {
  public void collectionSwap() {
    List<Integer> arr = Arrays.asList(1, 2, 3, 4, 5);
    System.out.println("정수 List 요소 위치 변경 전 : " + arr);

    Collections.swap(arr, 2, 4);
    System.out.println("정수 List 요소 위치 변경 후 : " + arr);
  }
}

/*
출력
정수 List 요소 위치 변경 전 : [1, 2, 3, 4, 5]
정수 List 요소 위치 변경 후 : [1, 2, 5, 4, 3]
*/

2. 배열을 Collections.swap() 메서드로 배열 내 요소 위치 변경하기

  • 일반 배열은 swap 메서드가 파라미터로 받지 않기 때문에 배열을 List 타입으로 바꿔줘야 합니다.
  • arr 배열을 swap 매서드 안에 Arrays의 asList안에 담아주고 인덱스1과 인덱스2를 입력해주면 됩니다.
package collectionsExample;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SwapMethod {
  public static void main(String[] args) {
    CollectionsSwap2 collectionsSwap2 = new CollectionsSwap2();
    collectionsSwap2.collectionsSwap();
  }
}

class CollectionsSwap2 {
  public void collectionsSwap() {
    String[] arr = {"a", "b", "c", "d", "e"};
    System.out.println("문자열 배열 요소 위치 변경 전 : " + Arrays.toString(arr));

    Collections.swap(Arrays.asList(arr), 2, 4);
    System.out.println("문자열 배열 요소 위치 변경 전 : " + Arrays.toString(arr));
  }
}

/*
출력
문자열 배열 요소 위치 변경 전 : [a, b, c, d, e]
문자열 배열 요소 위치 변경 전 : [a, b, e, d, c]
*/

3. 배열을 List로 변환 후 Collections.swap() 메서드로 배열 내 요소 위치 변경하기

  • 배열을 List로 만드는 또 다른 방법은 for문을 이용하여 배열 arr의 원소를 하나씩 꺼내어 List에 담아주고
  • 똑같이 swap 메서드에 List와 바꾸고 싶은 인덱스 번호를 입력해주면 됩니다.
package collectionsExample;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SwapMethod {
  public static void main(String[] args) {
    CollectionsSwap1 collectionsSwap = new CollectionsSwap1();
    collectionsSwap.collectionSwap();
  }
}

class CollectionsSwap3 {
  public void collectionsSwap() {
    int[] arr = {1, 2, 3, 4, 5};
    List<Integer> newArr = new ArrayList<>();
    System.out.println("정수 배열 요소 위치 변경 전 : " + Arrays.toString(arr));

    for (int i : arr) {
      newArr.add(i);
    }

    Collections.swap(newArr, 2, 4);
    System.out.println("정수 List로 변환, 요소 위치 변경 후 : " + newArr);
  }
}

/*
출력
정수 배열 요소 위치 변경 전 : [1, 2, 3, 4, 5]
정수 List로 변환, 요소 위치 변경 후 : [1, 2, 5, 4, 3]
*/

전체 코드

더보기
package collectionsExample;

import java.util.*;

public class SwapMethod {
  public static void main(String[] args) {
    CollectionsSwap1 collectionsSwap = new CollectionsSwap1();
    collectionsSwap.collectionSwap();

    CollectionsSwap2 collectionsSwap2 = new CollectionsSwap2();
    collectionsSwap2.collectionsSwap();

    CollectionsSwap3 collectionsSwap3 = new CollectionsSwap3();
    collectionsSwap3.collectionsSwap();
  }
}

class CollectionsSwap1 {
  public void collectionSwap() {
    List<Integer> arr = Arrays.asList(1, 2, 3, 4, 5);
    System.out.println("정수 List 요소 위치 변경 전 : " + arr);

    Collections.swap(arr, 2, 4);
    System.out.println("정수 List 요소 위치 변경 후 : " + arr);
  }
}

class CollectionsSwap2 {
  public void collectionsSwap() {
    String[] arr = {"a", "b", "c", "d", "e"};
    System.out.println("문자열 배열 요소 위치 변경 전 : " + Arrays.toString(arr));

    Collections.swap(Arrays.asList(arr), 2, 4);
    System.out.println("문자열 배열 요소 위치 변경 전 : " + Arrays.toString(arr));
  }
}

class CollectionsSwap3 {
  public void collectionsSwap() {
    int[] arr = {1, 2, 3, 4, 5};
    List<Integer> newArr = new ArrayList<>();
    System.out.println("정수 배열 요소 위치 변경 전 : " + Arrays.toString(arr));

    for (int i : arr) {
      newArr.add(i);
    }

    Collections.swap(newArr, 2, 4);
    System.out.println("정수 List로 변환, 요소 위치 변경 후 : " + newArr);
  }
}

/*
출력
정수 List 요소 위치 변경 전 : [1, 2, 3, 4, 5]
정수 List 요소 위치 변경 후 : [1, 2, 5, 4, 3]
문자열 배열 요소 위치 변경 전 : [a, b, c, d, e]
문자열 배열 요소 위치 변경 전 : [a, b, e, d, c]
정수 배열 요소 위치 변경 전 : [1, 2, 3, 4, 5]
정수 List로 변환, 요소 위치 변경 후 : [1, 2, 5, 4, 3]
*/