//法一
//常规做法:使用第三个变量
int temp;
temp = num1;
num1 = num2;
num2 = temp;
System.out.println("法一:交换后这两个变量的值是:" + num1 + " " + num2);
//法二:
//不使用第三个变量,使用两个变量的加减操作
num1 = num1 + num2;
num2 = num1 - num2; //num2 = num1 + num2 - num2 = num1;
num1 = num1 - num2; //num1 = num1 + num2 - num1 = num2;
System.out.println("法二:再次交换后这两个变量的值是:" + num1 + " " + num2);
//法三:
//不使用第三个变量,使用位操作:按位异或^:同0异1.
// 0和任何数做异或运算为此数本身,任何数与它自己做亦或为0.
num1 = num1 ^ num2;
num2 = num1 ^ num2; //num2 = num1^ num2 ^ num2 = num1
num1 = num1 ^ num2; //num1 = num1 ^ num2 ^ num1 = num2;
System.out.print("法三:再次交换后这两个变量的值是:" + num1 + " " + num2);
//法四:
//不使用第三个变量,使用赋值语句,一句话搞定.
num1 = (num1 + num2) - (num2 = num1) //表达式的值是赋值之后num2的值
System.out.print("法四:再次交换后这两个变量的值是:" + num1 + " " + num2);
注意:
只能在函数中输出交换后的值,不能通过在主函数中调用change(int num1,int num2)函数的方法改变两数的值。
因为java是值传递的,方法里面只是有一份值的拷贝
public class Test {
private static void swap (Integer a,Integer b) throws NoSuchFieldException, IllegalAccessException {
/*参考的下面的思路
Class aClass = a.getClass();
Field field= aClass.getDeclaredField(“username”);
field.setAccessible(Boolean.TRUE);
field.set(a,666);
System.out.println(a.getUsername() + " " + b.getUsername());
*/
Integer temp = new Integer(a);
Class aa = a.getClass();
Field aaa = aa.getDeclaredField("value");
aaa.setAccessible(Boolean.TRUE);
aaa.set(a,b);
Class bb = b.getClass();
Field bbb = bb.getDeclaredField("value");
bbb.setAccessible(Boolean.TRUE);
bbb.set(b, temp);
}
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Scanner input = new Scanner(System.in);
System.out.print("请输入要交换的两个数:");
Integer a = input.nextInt();
Integer b = input.nextInt();
swap(a, b); //抛出 NoSuchFieldException, IllegalAccessException 异常
System.out.println("交换后:a=" + a + ",b=" + b);
}
}
public class Test {
//私有字段; final修饰的字段; int、String等基本类型 均可通过反射来进行修改。
//但是反射不能修改 static 修饰的字段
static class User {
//将String类型全部改成int类型,其余内容做相应的变动,仍然可以改变A的值
//改成private final String username;其余内容不变,仍然输出同样的内容
private String username;
public User(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
//未设置set方法等任何可以改变其值的方法
}
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
User a = new User("A");
User b = new User("B");
System.out.println(a.getUsername() + " " + b.getUsername());
Class aClass = a.getClass();
Field field = aClass.getDeclaredField("username"); //抛出NoSuchFieldException异常
field.setAccessible(Boolean.TRUE);
field.set(a,"通过反射改掉A的值"); //抛出IllegalAccessException异常
System.out.println(a.getUsername() + " " + b.getUsername());
}
}
构造对象,将输入的两个数作为对象的属性,然后交换对象中的相应属性。
public class Test {
//交换ChangeNum类中的num1对象和num2对象中的两个属性值
static class ChangeNum {
private int num;
/*也可以通过设置set函数来设置对象的属性值
public void setNum(int num) {
this.num = num;
}
*/
//也可以通过重写toString()的方法输出
}
public static void swap(ChangeNum num1,ChangeNum num2 ) {
ChangeNum temp = new ChangeNum();
temp.num = num1.num;
num1.num = num2.num;
num2.num = temp.num;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入要交换的两个数:");
int digit1 = input.nextInt(),digit2 = input.nextInt();
ChangeNum num1 = new ChangeNum();
num1.num = digit1;
ChangeNum num2 = new ChangeNum();
num2.num = digit2;
System.out.println("交换前这两个数是:" + num1.num + "," + num2.num);
swap(num1,num2);
System.out.println("交换后这两个数是:" + num1.num + "," + num2.num);
}
}
public class Test {
//定义为静态方法
//如果不定义为静态方法,则在主函数中需要依赖于对象,需要通过对象来调用
public static void swap(int[] arr,int num1,int num2){
int temp = arr[num1];
arr[num1] = arr[num2];
arr[num2] = temp;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入您要交换的两个数值:");
int num1 = input.nextInt(),num2 = input.nextInt();
int[] arr= {num1,num2};
swap(arr,0,1);
System.out.println("交换后两个数为:" + arr[0] + "," + arr[1]);
}
}
错误原因:
java中如果传递的是对象,则是对象的引用值的拷贝。
只对这个拷贝进行操作,不会改变原来的引用关系。
想要交换值,需要修改引用指向的对象本身
public class Test {
private static void swap(Object num1, Object num2) {
Object temp = num1;
num1 = num2;
num2 = temp;
}
public static void main(String[] args) {
Object num1 = new Object();
Object num2 = new Object();
System.out.println(num1.hashCode() + " " + num2.hashCode());
swap(num1, num2);
System.out.println(num1.hashCode() + " " + num2.hashCode());
}
}
参考:java实现值交换(swap)