异或运算的巧用 → 不用额外的变量,如何交换两个变量的值?
public class xorExchange {
public static void main(String[] args) {
int [] arr={1,22,33,44,55,66};
swapWithoutOtherVar(arr,0,5);
}
public static void swapWithoutOtherVar(int[] arr, int i, int j) {
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
for (int k = 0; k < arr.length; k++) {
System.out.println("arr["+k+"]:"+arr[k]);
}
}
}
参考--https://www.cnblogs.com/youzhibing/p/15615272.html