。。没太听懂
大概可以用 “引用交换” (←自创名词)int[] ints = int[2];
int tmpInt;tmpInt = ints[0];
if(tmpInt > ints[1]) {
ints[0] = ints[1];
ints[1] = tmpInt; //交换
}是这样嘛?
大概可以用 “引用交换” (←自创名词)int[] ints = int[2];
int tmpInt;tmpInt = ints[0];
if(tmpInt > ints[1]) {
ints[0] = ints[1];
ints[1] = tmpInt; //交换
}是这样嘛?
int x, y;
void swap(){
x^=y;y^=x;x^=y;
}
}
new A().swap();2)This also uses the field members of A, but the method is called outside.class B{
static void swap(A a){
a.x^=a.y;a.y^=a.x;a.x^=a.y;
}
}B.swap(new A());3) For local variables,
int[] x;
x[i]^= x[j].....Method 1) is the best, as i've mentioned, because it sometimes does good.e.g.:
Class A{
int x, y;
boolean canSwap(){return x>y && y!=0;}
void swap(){
if(canSwap()){
.........
}
}
}Only by means of this can x and y, field members of A, be tested whether they can swap before they swap. The rest two will directly swap without any test, which may lead to a break-down.