# Swap in C C++ C# Java

C：

void swap(int i, int j)
{
int t = i;
i = j;
j = t;
}

void swap(int *i, int *j)
{
int t = *i;
*i = *j;
*j = t;
}

#define swap(type, i, j) {type t = i; i = j; j = t;}

#define swap(type, i, j) {type t = i; i = j; j = t;}

int main()
{
int a = 23, b = 47;
printf("Before swap. a: %d, b: %d\n", a, b);
swap(int, a, b)
printf("After swap.  a: %d, b: %d\n", a, b);
return 0;
}

int main()
{
int a = 23, b = 47;
printf("Before swap. a: %d, b: %d\n", a, b);
{ int t = a ; a = b ; b = t ; }
printf("After swap.  a: %d, b: %d\n", a, b);
return 0;
}

C++：

void swap(int& i, int& j)
{
int t = i;
i = j;
j = t;
}

C++的函数参数使用引用（&），值通过引用传递（pass by reference），函数中的参数不被 copy（如果传的是类就不会调用拷贝构造函数），所以在函数中能正确交换两个变量的值。

C#：

static void Swap<T>(ref T lhs, ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}

Java：

java.util.Collections;
public static void swap(List<?> list,int i,int j)

// MyInteger: similar to Integer, but can change value
class MyInteger {
private int x;                   // single data member
public MyInteger(int xIn) { x = xIn; } // constructor
public int getValue() { return x; }  // retrieve value
public void insertValue(int xIn) { x = xIn;} // insert
}

public class Swapping {
// swap: pass references to objects
static void swap(MyInteger rWrap, MyInteger sWrap) {
// interchange values inside objects
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);
}
}

C#这点和Java是一样的，C#版的交换如果不使用ref关键字，swap函数也没法正确工作。

public void tricky(Point arg1, Point arg2)
{
arg1.x = 100;
arg1.y = 100;
Point temp = arg1;
arg1 = arg2;
arg2 = temp;
}

public static void main(String [] args)
{
Point pnt1 = new Point(0,0);
Point pnt2 = new Point(0,0);
System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
System.out.println(" ");
tricky(pnt1,pnt2);
System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
}

———————————————————-
X: 0 Y: 0
X: 0 Y: 0

X: 100 Y: 100
X: 0 Y: 0
———————————————————-

Java复制并传递了“引用”的值，而不是对象。因此，方法中对对象的计算是会起作用的，因为引用指向了原来的对象。但是因为方法中对象的引用是“副本”，所以对象交换就没起作用。如下图所示，交换动作只对方法中的引用副本起作用了，不影响方法外的引用。所以不好意思，方法被调用后，改变不了方法外的对象的引用。如果要对方法外的对象引用做交换，我们应该交换原始的引用，而不是它的副本。

http://www.cs.utsa.edu/~wagner/CS2213/swap/swap.html
http://stackoverflow.com/questions/1363186/is-it-possible-to-write-swap-method-in-java
http://www.importnew.com/3559.html

http://www.cnblogs.com/luxiaoxun/p/3999583.html

+ 订阅