方法一
加减法
public static void exchange1(int a, int b) {
a = a + b;
b = a - b;
a = a - b;
System.out.println("加减法替换后 a = " + a + ", b = " + b);
}
方法二
乘除法
public static void exchange2(int a, int b) {
a = a * b;
b = a / b;
a = a / b;
System.out.println("乘除法替换后 a = " + a + ", b = " + b);
}
方法三
异或法
public static void exchange3(int a, int b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("异或法替换后 a = " + a + ", b = " + b);
}
方法四
使用数组
public static void exchange4(int a, int b) {
int[] arr = new int[]{a, b};
System.out.println("使用数组后 a = " + arr[1] + ", b = " + arr[0]);
}
方法五
使用map
public static void exchange5(int a, int b) {
Map<String, Integer> map = new HashMap<>();
map.put("a", a);
map.put("b", b);
a = map.get("b");
b = map.get("a");
System.out.println("使用map后 a = " + a + ", b = " + b);
}