请都高手,在java中传址怎么实现?

blueprogramer 2003-05-08 10:48:01
class Swap
{
public static void main(String args[])
{
Integer a, b;
int i,j;
a = new Integer(10);
b = new Integer(50);
i = 5;
j = 9;
System.out.println( "Before Swap, a is " + a);
System.out.println( "Before Swap, b is " + b);
swap(a, b);
System.out.println( "After Swap a is " + a);
System.out.println( "After Swap b is " + b);
System.out.println( "Before Swap i is " + i);
System.out.println( "Before Swap j is " + j);
swap(i,j);
System.out.println( "After Swap i is " + i);
System.out.println( "After Swap j is " + j);
}
public static void swap(Integer ia, Integer ib)
{
Integer temp = ia;
ia = ib;
ib = temp;
}
public static void swap(int li, int lj)
{
int temp = li;
li = lj;
lj = temp;
}
}
上面程序的输出是:
Before Swap, a is 10
Before Swap, b is 50
After Swap a is 10
After Swap b is 50
Before Swap i is 5
Before Swap j is 9
After Swap i is 5
After Swap j is 9

我所期望的是在我调用 swap函数后,a,b的值都改变,i,j也是都改变,实际上没有改变,
我应如何去做呢?请高手相助.



...全文
1140 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
feiyuegaoshan 2003-05-11
  • 打赏
  • 举报
回复
各位去好好读一下Java语言定义,
对于primative类型,传送的是value
对于object,传送的是reference value

请注意了:无乱primative还是object,传送的都是value。
所以,各位的想法都偏了。你只可能利用一些技巧达到传址的目的。但不可能得到Java语言本身机制上的支持。
blueprogramer 2003-05-08
  • 打赏
  • 举报
回复
楼上所说的文章我看过了,但是还是没有达到我的要求,
我想让这个,a与b交换值,i与j交换值,
哪位高手还有办法?
hellomartin 2003-05-08
  • 打赏
  • 举报
回复
去看楼上说的网址吧!
ajiao 2003-05-08
  • 打赏
  • 举报
回复
http://www.zdnet.com.cn/developer/code/story/0,2000081534,39049790,00.htm
yuanmeng163 2003-05-08
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=59569
littlecpu 2003-05-08
  • 打赏
  • 举报
回复
特殊问题特殊解决,只能这样假换址,(实值换内容量的值)。


public class Int
{
int i;

private void setI(int _i)
{
this.i = _i;
}

public Int(int _i)
{
this.i = _i;
}

private int getI()
{
return this.i;
}

public void swap(Int I)
{
int _i = I.getI();
I.setI(this.getI());
this.setI(_i);
}

public String toString()
{
return String.valueOf(i);
}
}

如果你不是碰到这样的问题要解决,用不着在这钻牛角。
MindActUponMind 2003-05-08
  • 打赏
  • 举报
回复
假设在上面得代码中加入:
///////////
......
////
MyClassTest c = new MyClassTest(1);
MyClassTest d = new MyClassTest(2);
System.out.println(c);
System.out.println(d);
swap(c,d);
System.out.println(c);
System.out.println(d);
///////
......
//////
public static void swap(MyClassTest c1,MyClassTest c2){
c1.i ++;
c2.i ++;
MyClassTest temp = c1;
c2 = c1;
c1 = temp;

}

class MyClassTest{
int i;
public MyClassTest(int i){
this.i = i;
}
public String toString(){
return i +"";
}
}


当然,交换是不成功的.但是,应该注意到c和d的值均发生了变化。
那么,这里到底传的什么???
ChDw 2003-05-08
  • 打赏
  • 举报
回复
其实Java对于对象就是传递地址的
public static void swap(Integer ia, Integer ib)
{
Integer temp = ia;
ia = ib;
ib = temp;
}
但是你这种更换其实是已经改变了ia,ib所指向的地址了,而不是改变内容!
Java里面是不允许作出这样的改变的,你只能够通过其他的一些方法间接实现
feiyuegaoshan 2003-05-08
  • 打赏
  • 举报
回复
Java中所有东西都是传值。
所以,你不要再尝试了:)。
coldplay 2003-05-08
  • 打赏
  • 举报
回复
class fuck
{
public fuck()
{
a = this.a;
b = this.b;

}
public int a = 3;
public int b = 4;


}

public class TestArg
{
public static void main(String[] args)
{
fuck f = new fuck();
System.out.println(f.a);
System.out.println(f.b);
swap(f);
System.out.println(f.a);
System.out.println(f.b);

}

public static void swap(fuck x)
{
int temp;
temp = x.a;
x.a = x.b;
x.b = temp;
}
}
希望对你有帮助

62,615

社区成员

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

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