程序员的悲伤!!!

让两个数相互交换最简单的方法是让他们交融,先做到你中有我我中有你,接下来我失去我,你失去你,那么他们自然就交换了
m=m+n;n=m-n;m=m-n;
a=a^b;b=a^b;a=a^b;
这无需第三者介入
两个人只要你想着我,我想着你就行,就不要分离
但我TM单身

看到一段代码 用Java反射实现两数交换

public class Swap {
    static void swap(Integer a,Integer b) throws  NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
        Field f=Integer.class.getDeclaredField("value");
        f.setAccessible(true);
        int temp=new Integer(a.intValue());
        f.setInt(a, b.intValue());
        f.setInt(b, temp);
    }
}

发布了15 篇原创文章 · 获赞 3 · 访问量 2231

猜你喜欢

转载自blog.csdn.net/sky_s_limit/article/details/80634841