【ylchou】通过代码完成2个整数内容的交换
2016-11-14 12:50
190 查看
通过代码完成2个整数内容的交换
不磨叽了,直接上代码:
[java] view plain copy
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,示例如下:
[java] view plain copy
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
不磨叽了,直接上代码:
[java] view plain copy
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,示例如下:
[java] view plain copy
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
相关文章推荐
- 通过代码完成2个整数内容的交换
- 3.7.2 通过代码完成两个整数内容的交换。
- javascript Table 中2个列(TD)的交换实现代码
- 写正确函数需要注意的地方:两个序列a,b,大小都为n,序列元素的值为任意整数,无序;要求通过交换a,b中的元素,使序列a元素的和与序列b元素的和之间的差最小
- C#中数组内容前后交换代码
- 一段通过OLE将Notes View中的内容导入Excel的示范代码
- 通过代码自定义cell——实现qq聊天界面(Version 2 实现聊天内容的背景)
- [微软]有两个序列a,b,大小都为n,序列元素的值任意整数,无序; 要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小
- [微软]有两个序列a,b,大小都为n,序列元素的值任意整数,无序; 要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小
- select列表左右内容交换的JS代码,支持同时多选
- select列表内容交换的js实现代码
- 今天的c++练习代码———>"欧几里德算法+函数的递归解决求2个整数的最大公约数"
- 通过异或交换数据内容。
- 通过代码获取sharepoint2010的“我喜欢(I like it)”、“标签(Tags and Notes)”、“记事本”以及“文档等级”活动内容
- javascript通过位运算完成两个数的交换
- javascript Table 中2个列(TD)的交换实现代码
- 一个页面显示2个数据库的查询内容 代码优化
- 不使用第三个变量完成两个整数的交换
- ContentProvider学习心得(中)--通过单元测试的方式完成对通讯录内容的操作
- Android中通过代码读取xml文件中的内容