通过代码完成2个整数内容的交换
2013-06-30 16:10
309 查看
通过代码完成2个整数内容的交换
不磨叽了,直接上代码:
console print:
i,j:23,55
i,j:55,23
x,y:44,77
x,y:77,44
0
----------------------------------------------------------------------------------------
扩展:要是字符串交换,用method two,示例如下:
console print:
str3,str4:abc,xyz
str3,str4:xyz,abc
null
不磨叽了,直接上代码:
package com.hylink.common; public class Swap { /** * @function 2个整数内容交换 * @author ylchou@qq.com * @param args */ public static void main(String[] args) { //method one 不用额外的变量来交换,不过可能回溢出 int i = 23; int j = 55; System.out.println("i,j:"+i+","+j); i = i + j;//右边为i和j之和 可能回溢出 j = i - j;//右边为为i的值 i = i - j;//右边为j的值 System.out.println("i,j:"+i+","+j); //method two 用额外的变量来临时存储交换,不会发生溢出 int x = 44; int y = 77; System.out.println("x,y:" + x + "," + y); int tmp = 0; tmp = x; x = y; y = tmp; tmp = 0; System.out.println("x,y:" + x + "," + y); System.out.println(tmp); } }
console print:
i,j:23,55
i,j:55,23
x,y:44,77
x,y:77,44
0
----------------------------------------------------------------------------------------
扩展:要是字符串交换,用method two,示例如下:
package com.hylink.common; public class Swap { /** * @function 字符串内容交换 * @author ylchou@qq.com * @param args */ public static void main(String[] args) { //字符串交换 用额外的变量来临时存储交换 String str3 = "abc"; String str4 = "xyz"; System.out.println("str3,str4:"+str3+","+str4); String tmpStr = null; tmpStr = str3; str3 = str4; str4 = tmpStr; tmpStr = null; System.out.println("str3,str4:"+str3+","+str4); System.out.println(tmpStr); } }
console print:
str3,str4:abc,xyz
str3,str4:xyz,abc
null
相关文章推荐
- 【ylchou】通过代码完成2个整数内容的交换
- 3.7.2 通过代码完成两个整数内容的交换。
- javascript通过位运算完成两个数的交换
- 交换调用函数中两个整数类型存储区的内容
- javascript Table 中2个列(TD)的交换实现代码
- 不通过 您的代码已保存 编译错误:您提交的代码无法完成编译 ./TreeLevel.java:37: error: cannot find symbol node.next=new ListNode(
- 有两个序列a,b,大小都为n,序列元素的值任意整数,无序;要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小。
- 利用KVO实现 UITextView 通过代码方式设置内容后,自动滚动到最后一行
- Interger类中进制转换方法的实现原理-通过查表法完成整数的进制转换
- Jquery多选框互相内容交换的实例代码
- 通过Javascript读取本地Excel文件内容的代码示例
- # include <errno.h >查看错误代码errno是调试程序的一个重要方法。当Linux C API函数发生异常时,一般会将errno变量赋值一个整数,不同的值表示不同的含义,可以通过查看
- 通过js实现2个select控件的交换数据
- ob_start(&quot;ob_gzhandler&quot;)与ob_*配合使用时导致浏览无法识别内容/开启gzip时且测试通过的下载代码
- C语言指针-------通过调用函数,完成两个数值的交换
- 通过交换相邻数来完成排序所需要的最少交换次数
- 有两个序列a,b,大小都为n,序列元素的值任意整数,无序;要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小。
- select列表内容交换的js实现代码
- 有两个序列a,b,大小都有n,序列元素的值任意整数,无序; 要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b无素的和]之间的差最大。