大家都知道,C/C++非常用日实现交换两个数的函数,因为我们可以通过指针或者饮用来做为参数。但是对于Java来说,对于基本数据类型,Java传递的是一个拷贝副本,根本不能改变参数的值。目前有两个方法,一个采用Wrapper来包装基本数据类型,传递对象,但是这些标准wrapper类不允许改变指,我们需要重写这样Wrapper类。另外的方法是通过数组来完成,因为数组传递的是地址。首先看一下,传递基本数据类型的例子。
/**
* In Java, the primitive parameters are the copy values, didn't change the original values by this way.
* @param x
* @param y
*/
public static void swap1(int x, int y){
int temp = y;
y=x;
x=temp;
}
打印结果没有改变int x =5, y =6;
swap1(x,y);
System.out.println("x="+x);
System.out.println("y="+y);x=5
y=6
就上面的方法1,我们通过重写Wrapper类实现。
class myInteger {
private int value;
public myInteger(int x){ value = x;}
public int getValue(){ return value; }
public void insertValue(int x){ value = x; }
}
/**
* In Java, actually we can use wrapper objects as parameter, by this way, it will pass the address of the object.
* But Integer can't be changed a value, so we overload the default Integer class.
* @param x
* @param y
*/
public static void swap3(myInteger x, myInteger y){
int temp = y.getValue();
y.insertValue(x.getValue());
x.insertValue(temp);
}打印结果:
myInteger xI = new myInteger(x);
myInteger yI = new myInteger(y);
swap3(xI, yI);
System.out.println("x=" + xI.getValue());
System.out.println("y=" + yI.getValue());
x=6
y=5就上门的方法2,通过数组完成,参看代码。
/**
*
* @param a - array, will pass the first element address of this array
* @param x - position of this array, not value
* @param y - position of this array, not value
*/
public static void swap2(int[] a, int x, int y)
{
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
int[] a = new int[2];
a[0] = x;
a[1] = y;
swap2(a, 0, 1);
System.out.println("x=" + a[0]);
System.out.println("y=" + a[1]);打印结果:
x=6
y=5这两方方法我觉得都比较麻烦,又没有更棒的方法,让人看了激动地想哭的方法,求高手献招!!谢谢了!!Javaswap算法
/**
* In Java, the primitive parameters are the copy values, didn't change the original values by this way.
* @param x
* @param y
*/
public static void swap1(int x, int y){
int temp = y;
y=x;
x=temp;
}
打印结果没有改变int x =5, y =6;
swap1(x,y);
System.out.println("x="+x);
System.out.println("y="+y);x=5
y=6
就上面的方法1,我们通过重写Wrapper类实现。
class myInteger {
private int value;
public myInteger(int x){ value = x;}
public int getValue(){ return value; }
public void insertValue(int x){ value = x; }
}
/**
* In Java, actually we can use wrapper objects as parameter, by this way, it will pass the address of the object.
* But Integer can't be changed a value, so we overload the default Integer class.
* @param x
* @param y
*/
public static void swap3(myInteger x, myInteger y){
int temp = y.getValue();
y.insertValue(x.getValue());
x.insertValue(temp);
}打印结果:
myInteger xI = new myInteger(x);
myInteger yI = new myInteger(y);
swap3(xI, yI);
System.out.println("x=" + xI.getValue());
System.out.println("y=" + yI.getValue());
x=6
y=5就上门的方法2,通过数组完成,参看代码。
/**
*
* @param a - array, will pass the first element address of this array
* @param x - position of this array, not value
* @param y - position of this array, not value
*/
public static void swap2(int[] a, int x, int y)
{
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
int[] a = new int[2];
a[0] = x;
a[1] = y;
swap2(a, 0, 1);
System.out.println("x=" + a[0]);
System.out.println("y=" + a[1]);打印结果:
x=6
y=5这两方方法我觉得都比较麻烦,又没有更棒的方法,让人看了激动地想哭的方法,求高手献招!!谢谢了!!Javaswap算法
Collections.swap(l, 0, 1);
原来缘来
是我的笔误, 应该是C/C++非常容易实现,哈哈.
不错的方法,不过和数组的方法相同的原理, 先将数据添加List(),在进行交换,开来只能如此了.Bravo!!
List<Integer> l = Arrays.asList(x, y);
System.out.println("x="+ l.get(0).intValue());
System.out.println("y="+ l.get(1).intValue());
Collections.swap(l, 0, 1);
System.out.println("x="+ l.get(0).intValue());
System.out.println("y="+ l.get(1).intValue());
public int[] swap(int x,int y){
return new int[]{y,x};
}
x = x+y;
y = x-y;
x = x-y;
x = x^y;
y = x^y;
x = x^y;
class Test{
int x ;
int y ;
}
public class Main {
public static void main(String args[]){
Test t = new Test();
t.x = 3 ;
t.y = 4 ;
swap(t);
//swap(t,t.x,t.y);
System.out.println(t.x+","+t.y);
} public static void swap(Test t) {
t.x = t.x+t.y ;
t.y = t.x - t.y ;
t.x = t.x - t.y ;
}
public static void swap(Test t, int x, int y) {
t.x = x + y;
t.y = t.x - y ;
t.x = t.x - x ;
}
//其实传对象和传数组的方法类似
}