我看你问题里说的是 交换两个Integer类型, 按理这里传了两个对象的引用, 可以对 对象本身做一些操作. BUT, Integer对象是不可变的, 它的API没有任何对内部值最修改的 方法; 看一下源码也知道, 内部的int也是final的:private final int value;
如果我们非要做 对int的 swap, 那么只有自己定义个 int型的 wrapper了:
class MyInteger{
private int value;
public MyInteger(int i) {this.value=i;}
public void setValue(int value){this.value=value;}
public int getValue(){return this.value;}
public static void swapMyInteger(MyInteger a, MyInteger b){
MyInteger c=new MyInteger(a.getValue());
a.setValue(b.getValue());
b.setValue(c.getValue());
}
}