java中2个数据的交换如何实现??
class pt
{
int x,y;
pt(int x,int y)
{
this.x=x;
this.y=y;
}
}
////////////////////////////////////////////////////////
class test
{
static void swap(int m,int n)
{
int temp;
temp=m;
m=n;
n=temp;
}
static void change(pt a,pt b)
{
pt temp;
temp=a;
a=b;
b=temp;
}
public static void main(String[] args)
{
int m1=1;
int n1=2;
swap(m1,n1);
System.out.println(m1);
System.out.println(n1);
pt p1=new pt(1,2);
pt p2=new pt(3,4);
change(p1,p2);
System.out.println(p1.x);
System.out.println(p1.y);
System.out.println(p2.x);
System.out.println(p2.y);
}
}
//输出1 2 1 2 3 4,问想输出2 1 3 4 1 2要怎么写??忘高手指点~~~
------解决方案--------------------class pt {
int x, y;
pt(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Test {
void change(pt p)
{
int temp;
temp=p.x;
p.x=p.y;
p.y=temp;
}
public static void main(String[] args) {
Test h = new Test();
int a = 1, b = 2;
pt p = new pt(a,b);
System.out.println(p.x + " " + " " + p.y);
h.change(p);
System.out.println(p.x + " " + " " + p.y);
}
}
参照一下吧
------解决方案--------------------java中所有的函数参数传递都是值传递
------解决方案--------------------学习下~~~~~~~~
------解决方案--------------------这是C#里面的 吧