public class SwapTest {
static class Pair<F, S> {
public F first
;
public S second
;
public Pair(F first
, S second
) {
this.first
= first
;
this.second
= second
;
}
@Override
public String
toString() {
return "Pair{" + String
.valueOf(first
) + " " + String
.valueOf(second
) + "}";
}
}
public static void swap1(Pair
<Integer, Integer> pair
) {
int tmp
= pair
.first
;
pair
.first
= pair
.second
;
pair
.second
= tmp
;
}
public static void swap2(Pair
<Integer, Integer> pair
) {
pair
.first
= pair
.first
- pair
.second
;
pair
.second
= pair
.first
+ pair
.second
;
pair
.first
= pair
.second
- pair
.first
;
}
public static void swap3(Pair
<Integer, Integer> pair
) {
pair
.first
= pair
.first
^ pair
.second
;
pair
.second
= pair
.first
^ pair
.second
;
pair
.first
= pair
.first
^ pair
.second
;
}
public static void main(String
[] args
) {
Pair
<Integer, Integer> p1
= new Pair<>(1, 2);
Pair
<Integer, Integer> p2
= new Pair<>(3, 4);
Pair
<Integer, Integer> p3
= new Pair<>(5, 6);
swap1(p1
);
swap2(p2
);
swap3(p3
);
System
.out
.println(p1
);
System
.out
.println(p2
);
System
.out
.println(p3
);
}
}
转载请注明原文地址: https://mac.8miu.com/read-504059.html