我是靠谱客的博主 优雅项链,这篇文章主要介绍Integer 引用传递交换值,现在分享给大家,希望可以做个参考。

 public static void swap(Integer i1, Integer i2) {
try {
Field field = Integer.class.getDeclaredField("value");
field.setAccessible(true);
Integer tmp = new Integer(i1.intValue());
field.set(i1, i2.intValue());
field.set(i2, tmp);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Integer i1 = 1;
Integer i2 = 2;
swap(i1, i2);
System.out.println(i1);
System.out.println(i2);
}

最后

以上就是优雅项链最近收集整理的关于Integer 引用传递交换值的全部内容,更多相关Integer内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(165)

评论列表共有 0 条评论

立即
投稿
返回
顶部