当前页面: 开发资料首页 → J2SE 专题 → 用函数交换两个变量的值
用函数交换两个变量的值
摘要: 用函数交换两个变量的值
void swap(int x,int y ){
int temp;
temp = x;
x = y;
y = temp;
}
这样肯定是不行的了,应该怎么做?
如果这两个变量是基本数据类型(int、float、double、char、long、short、byte、boolean)的话,做不到;类类型的话,可以。
void swap(int x,int y ){
int temp;
temp = x;
x = y;
y = temp;
}
楼上的楼上正解。用值构造一个Integer对象,然后用函数交换对象,再从对象里获取值