java如何交换两个整数包装器对象
如何交换两个整数包装的内容
void swap(Integer a,Integer b){
/*We can't use this as it will not reflect in calling program,and i know why
Integer c = a;
a= b;
b = c;
*/
//how can i swap them ? Does Integer has some setValue kind of method?
//if yes
int c = a;
a.setValue(b);
b.setValue(c);
}
# 1 楼答案
输出: 1. 2. 2. 一,
# 2 楼答案
类型
java.lang.Integer
表示一个永远不会改变其值的不可变的数。如果您想要一个可变的数字,请从Apache Commons中尝试MutableInt
您可以使用
List<T>
编写类似的代码,但您总是需要一个(或两个)容器来交换东西