C/C++??Java??swap???????
???????????? ???????[ 2015/10/19 10:52:51 ] ??????????????????? ????????
??????????????????c/c++??swap????
????void swap ( int & a?? int & b)
????{
????int Temp;
????temp = a;
????a = b;
????b = temp;
????}
?????????java????????????????????java????е?????÷??????
??????????????????c/c++??java????
??????????
????C/C++??swap??????????????????????(?????????)??????????????е????
????Java????ж?C/C++??????????????Java?????????????????????????????????á?
??????Java??????????????з???????????????????????????д????
??????????(char??int??double??????????????????????????
?????????????????????????(??C++?????ò??)?????????????????????????????????????????????????????????????????????????????????и??
?????????????????????????????java??????????????????java???????????(pass-by-value)??
????java??????????????????????????????????????
????????н????????????????????????????????????
????Method1???????齻??????????????е????????????????????????п?????
????Public static void swap ( int [] Data?? int a?? int b) {
????int t = Data [a];
????data [a] = data [b];
????data [b] = t;
????}
????Method2??????????????????
Class MyInteger {
Private int x; / / the x as the only data member
Public MyInteger ( int xIn) {x = xIn;} / / Constructor
Public int getValue () { return x;} / / get the value
Public void insertValue ( int xIn) {x = xIn;} / / change the value of the
}
Public Class Swapping {
/ / Swap: pass object references
static void swap (MyInteger rWrap?? MyInteger sWrap) {
/ / Change the value of the process
int t = rWrap.getValue ();
rWrap.insertValue (sWrap.getValue ());
sWrap.insertValue (t);
}
Public static void main (String [] args) {
int a = 23 ?? b = 47 ;
System.out.println ( "Before. a:" + a + "?? b:" + b);
MyInteger AWRAP = new MyInteger (a);
MyInteger bWrap = new MyInteger (b);
swap (aWrap?? bWrap);
a = aWrap.getValue ();
b = bWrap.getValue ();
System.out.println ( "After. a:" + a + "?? b:" + b);
}
}
????Method3??????????
Public Class Swap2 {
Public static void main (String args []) {
Swap2 SW = new Swap2 ( 1 ?? 2 );
System.out.println ( "i is" + sw.i);
System.out.println ( "J is" + sw.j);
sw.swap ();
System.out.println ( "i is" + sw.i);
System.out.println ( "J is" + sw.j);
}
int i?? J;
Public Swap2 ( int i?? int J) {
this . i = i;
this . J = J;
}
Public void swap () {
int Temp;
temp = i;
i = j;
j = temp;
}
}
Public Class swap1 {
Public static void swap1 (Integer a?? Integer b) {
Integer temp = a;
a = b;
b = temp;
}
Public static void main (String args []) {
Integer a?? b;
a = new Integer ( 10 );
b = new Integer ( 20 );
Swap1.Swap1 (a?? b);
System.out.println ( "a is" + a);
System.out.println ( "b is" + b);
}
}
???????
?????????е????????????????????е?public static void swap(int[] data?? int a?? int b)??????
???????????е???????????????????????????????????????????????????????????????????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11