我是Java新手。如何编写等效于以下C代码的java。void Swap(int *p, int *q){ int temp; temp = *p; *p = *q; *q = temp;}
3 回答

繁花如伊
TA贡献2012条经验 获得超12个赞
这是一种使用按位XOR(^)运算符仅在一行中交换Java 中的两个变量的方法。
class Swap
{
public static void main (String[] args)
{
int x = 5, y = 10;
x = x ^ y ^ (y = x);
System.out.println("New values of x and y are "+ x + ", " + y);
}
}
输出:
x和y的新值为10、5
添加回答
举报
0/150
提交
取消