In C/C ++, we can use pointers or references to solve the problem, such as the following C code:
Void swap (int * a, int * B)
...{
Int temp = *;
* A = * B;
* B = temp;
}
Or the following C ++ code:
Void swap (Int & A, Int & B)
...{
Int temp =;
A = B;
B = temp;
}
However, it is troublesome to implement this function in Java.
You can try it first.
I will describe how to implement this function below:
Class wrappednumber <t extends number> ...{
Private t I = NULL;
Public wrappednumber (t I )...{
This. I = I;
}
Public t getnumber ()...{
Return I;
}
Public void setnumber (t I )...{
This. I = I;
}
}
Public class hello ...{
Public static <t extends number> void swap (wrappednumber <t> A, wrappednumber <t> B )...{
T temp1 = A. getnumber ();
A. setnumber (B. getnumber ());
B. setnumber (temp1 );
}
Public static void main (string [] ARGs )...{
Wrappednumber <short> A = new wrappednumber <short> (short) 10 );
Wrappednumber <short> B = new wrappednumber <short> (short) 20 );
Hello. <short> swap (A, B );
System. Out. println ("the answer is: a =" + A. getnumber () + ", B =" + B. getnumber ());
}
}