想交换两个基本数据类型数据,如
int a = 89;
int b = 43;
swap(a, b);
输出 a = 43, b = 89
请问该swap(int, int)该如何实现
------解决方案--------------------------------------------------------
自己写个Integer的封装类吧。。。
- Java code
package cn.sky;public class Integer { private int num; public Integer(int num) { this.num = num; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } @Override public String toString() { return num + ""; }}class Test { public static void main(String[] args) { Integer a = new Integer(89); Integer b = new Integer(43); swap(a,b); System.out.println(a + "---" + b); } static void swap(Integer a, Integer b) { int temp = b.getNum(); b.setNum(a.getNum()); a.setNum(temp); }}