交换两个Integer的值
作者:
zhengxc | 来源:发表于
2018-09-20 19:26 被阅读0次 public static void main(String[] args) throws Exception {
Integer a = 128;
Integer b = 130;
System.out.println("a: " + a + " b: " + b);
swap(a, b);
System.out.println("a: " + a + " b: " + b);
}
private static void swap(Integer i, Integer j) throws Exception {
Field field = Integer.class.getDeclaredField("value");
field.setAccessible(true);
int tmp = j;
field.set(j, i); //将j对象的value字段设置为i
field.set(i, tmp); //将i对象的value字段设置为tmp
}
本文标题:交换两个Integer的值
本文链接:https://www.haomeiwen.com/subject/hznnnftx.html
网友评论