class Main {
public static int swap(int... args) { return args[0]; }
public static void main(String[] args) {
int a = 5;
int b = 10;
a = swap(b, b = a); // a = 10, b = 5
}
}
Swapping array elements - 교환할 값이 배열의 요소이면 배열인덱스로 접근해서 교환하는 메서드를 짤 수 있다.
class Main {
public static void swap(int[] arr int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5 };
swap(arr, 2, 3);
}
}
Swapping Objects, AtomicInteger - 객체로 wrapping해서 내부 값을 교환하는 방식
class Main {
public static void swap(IntRef a, IntRef b) {
int tmp = a.val;
a.val = b.val;
b.val = tmp;
}
public static void main(String[] args) {
IntRef a = new IntRef(5);
IntRef b = new IntRef(10);
swap(a, b); // a.val = 10, b.val = 5
}
}
java.util.concurrent.atomic.AtomicInteger로 wrapping할 수도 있다.
import java.util.concurrent.atomic.AtomicInteger;
class Main {
public static void swap(AtomicInteger a, AtomicInteger b) {
a.set(b.getAndSet(a.get()));
}
public static void main(String[] args) {
AtomicInteger a = new AtomicInteger(5);
AtomicInteger b = new AtomicInteger(10);
swap(a, b);
}
}
Collections.swap(list, i, j);
public static void swap(List<?> list, int i, int j)