求高手用Java实现swap(x, y),目前能想到两个方法,还能更多吗?

blacksilk 2013-06-25 09:14:23
大家都知道,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

这两方方法我觉得都比较麻烦,又没有更棒的方法,让人看了激动地想哭的方法,求高手献招!!谢谢了!!

...全文
553 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
blacksilk 2013-07-03
  • 打赏
  • 举报
回复
楼上利用对象变量完成交换,在Java中对象变量是传递对象地址的,不错的想法。
lagel 2013-07-03
  • 打赏
  • 举报
回复
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 ; } //其实传对象和传数组的方法类似 }
lagel 2013-07-03
  • 打赏
  • 举报
回复
还有就是把x,y设为全局变量,那么你的第一种方法就能使用了.
linkin1005 2013-07-02
  • 打赏
  • 举报
回复
引用 10 楼 oh_Maxy 的回复:
不用额外的变量: x = x+y; y = x-y; x = x-y;
溢出~
茫茫大海 2013-07-01
  • 打赏
  • 举报
回复
2到20个英文 2013-07-01
  • 打赏
  • 举报
回复
楼上厉害,这个也可以不用额外的变量 x = x^y; y = x^y; x = x^y;
oh_Maxy 2013-07-01
  • 打赏
  • 举报
回复
不用额外的变量: x = x+y; y = x-y; x = x-y;
Inhibitory 2013-06-30
  • 打赏
  • 举报
回复
Lua交换两个数更容易:x, y = y, x
wo111180611 2013-06-30
  • 打赏
  • 举报
回复
可通过按位异或 a^=b; b^=a; a^=b;
karl 2013-06-29
  • 打赏
  • 举报
回复
在外面交换就行了,为啥要弄个函数呢,实在不行你就 public int[] swap(int x,int y){ return new int[]{y,x}; }
blacksilk 2013-06-26
  • 打赏
  • 举报
回复
回复:czl24boy 不错的方法,不过和数组的方法相同的原理, 先将数据添加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());
blacksilk 2013-06-26
  • 打赏
  • 举报
回复
回复JayYounger 原来缘来 是我的笔误, 应该是C/C++非常容易实现,哈哈.
czl24boy 2013-06-25
  • 打赏
  • 举报
回复
List<Integer> l = Arrays.asList(5, 6); Collections.swap(l, 0, 1);
原来缘来 2013-06-25
  • 打赏
  • 举报
回复
大家都知道,C/C++非常用日实现交换两个数的函数,因为我们可以通过指针或者饮用来做为参数 大家很少知道
地下室森林 2013-06-25
  • 打赏
  • 举报
回复
现在没有比较好的想法。一起期待“It may our cry”的代码出现吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧