62,615
社区成员
发帖
与我相关
我的任务
分享
public class SwapObject {
public static void main(String[] args){
Object o1 = new Object();
Object o2 = new Object();
System.out.println("o1:"+o1.hashCode());
System.out.println("o2:"+o2.hashCode());
swap(o1, o2);//不可以
System.out.println("o1:"+o1.hashCode());
System.out.println("o2:"+o2.hashCode());
}
public static void swap(Object o1,Object o2) {
Object o = new Object();
o = o1;
o1 = o2;
o2 = o ;
}
}
public class SwapObject {
public static void main(String[] args){
Object[] oArray = new Object[2];
oArray[0] = new Object();
oArray[1] = new Object();
System.out.println("o1:"+oArray[0].hashCode());
System.out.println("o2:"+oArray[1].hashCode());
swapArray(oArray);
System.out.println("o1:"+oArray[0].hashCode());
System.out.println("o2:"+oArray[1].hashCode());
}
public static void swapArray(Object[] oArray){
Object o = new Object();
o = oArray[0];
oArray[0] = oArray[1];
oArray[1] = o;
}
}