Java Software Programming Examples For Beginners / Interview: How To Swap Integer (and String) Variables Without Using a Temporary Variable Java软件编程的例子,对于初学者/面试:如何交换整数(和字符串)变量,而不使用一个临时变量
Say x & y are the integer variables.说x与y是整数的变数。 The challenge is to swap them without using a temporary variable.所面临的挑战是要交换他们不使用临时变量。
The solution is as simple as the problem itself:解决的办法是那么简单的问题本身:
x = x + y; x =用X + y ;
y = x - y;为Y = x -y ;
x = x - y; x = x -y ;
Update 1: Jack and Alexey pointed out a typo in my solution which has since been corrected.更新1 :杰克和阿列克谢指出,错字在我的解决方案,其中已被纠正。 Please see their comments below.请参阅他们的意见如下。
The simplicity of the solution appeals to me.简单的解决方案呼吁我。 It clearly demonstrates the meaning of assignment operator ( “=” ).它清楚地表明所指的转让的营办商( “ = ” ) 。
What if they are Strings?如果他们是字符串呢?
Note: You can use String methods in Java API.注意:您可以使用字符串的方法的Java API 。
Here is the solution:这里是解决方法:
x = x + y; x =用X + y ;
y = x.substring(0, x.indexOf(y));为Y = x.substring ( 0 , x.indexof (元Y ) ) ;
x = x.substring(x.indexOf(y) + y.length()); x = x.substring ( x.indexof (元Y ) + y.length ( ) ) ;
Can you see the similarity?你能看到的相似性呢?
Can you provide a simpler solution to either of the above?你能提供一个更简单的解决办法上述任一?
Update 2: Robert just did (see his comment below).更新2 :罗伯特只是没有(见他的评论下文) 。 He pointed out that the String solution doesn’t work when one string is contained in another.他指出,该字符串的解决办法行不通时,一个字符串是载于另一个。 His solution is:他的解决办法是:
x = x + y; x =用X + y ;
y = x.substring(0, x.length() - y.length());为Y = x.substring ( 0 , x.length ( ) -y .length( ) ) ;
x = x.substring(y.length()); x = x.substring ( y.length ( ) ) ;
Filed under提起下 How To如何 , , Java Software Java软件 , , Tech Note技术说明 | |
| |
RSS 2.0 2.0 | |
Trackback Trackback跟踪 this Article |此文章|
Email this Article电子邮件此文章
You may also like to read您也可以想读 |




October 30th, 2005 at 2:53 am 2005年10月30日在上午02时53分
x : 1 y : 2 x : 1 y : 2
x = x + y; x =用X + y ;
x : 3 y : 2 x : 3 y : 2
x = x - y; x = x -y ;
x : 1 y : 2 x : 1 y : 2
y = x - y;为Y = x -y ;
x : 1 y : -1 x : 1 y : -1
October 30th, 2005 at 3:04 am 2005年10月30日在上午03时04分
x = x+y; x =用X + y ;
y = xy;为Y =的XY ;
x = xy; x =的XY ;
October 30th, 2005 at 8:36 am 2005年10月30日在上午8时36分
Maybe大概
x = x + y; x =用X + y ;
y = x - y;为Y = x -y ;
x = x - y; x = x -y ;
October 30th, 2005 at 9:24 am 2005年10月30日在上午09时24分
@Jack & Alexey @杰克&阿列克谢
Oops!糟糕!
Thanks for the catch.感谢的渔获量。 Corrected.纠正。
Just checking if you are attentive刚才检查如果您是细心的
October 30th, 2005 at 9:25 am 2005年10月30日在上午09时25分
BTW: I actually asked this question today while interviewing candidates.的BTW :其实我问这个问题,而今天的面试人选。
October 31st, 2005 at 8:15 am 2005年10月31日在上午8时15分
The string sample will fail if the string x contains the string y.字符串的样本会失败,如果字符串x包含字符串耀 For example,举例来说,
String x = “Hello World”;字符串x = “世界您好” ;
String y = “Hello”;字符串为Y = “你好” ;
Step 1: x = x + y;第1步: x =用X + y ;
x = “Hello WorldHello” x = “您好worldhello ”
Step 2: y = x.substring(0, x.indexOf(y));第2步为: Y = x.substring ( 0 , x.indexof (元Y ) ) ;
x.indexOf(y) = 0, so y = “”; x.indexof (元Y ) = 0 ,所以为Y = “ ” ;
Step 3: x = x.substring(x.indexOf(y) + y.length());第3步: x = x.substring ( x.indexof (元Y ) + y.length ( ) ) ;
x.indexOf(y) = 0 and y.length = 0, so x = “Hello WorldHello” x.indexof (元Y ) = 0和y.length = 0 ,所以x = “您好worldhello ”
You can fix this by using lastIndexOf() method in String like您可以修复此用lastindexof ( )方法在字符串一样
x = x + y; x =用X + y ;
y = x.substring(0, x.lastIndexOf(y));为Y = x.substring ( 0 , x.lastindexof (元Y ) ) ;
x = x.substring(x.indexOf(y) + y.length()); x = x.substring ( x.indexof (元Y ) + y.length ( ) ) ;
or you could eliminate the indexOf() calculations altogether and just use the lengths of the strings或者你可以消除索引( )计算,合共只需使用长度的字符串
x = x + y; x =用X + y ;
y = x.substring(0, x.length() - y.length());为Y = x.substring ( 0 , x.length ( ) -y .length( ) ) ;
x = x.substring(y.length()); x = x.substring ( y.length ( ) ) ;
October 31st, 2005 at 8:53 am 2005年10月31日在上午8时53分
@Robert @罗伯特
I like that.我喜欢这一点。 It looks cleaner.看来,清洁。
November 9th, 2005 at 2:08 am 2005年11月9日在上午02时08分
Unfortunately, the code to exchange integer values will fail for values that cause over- or underflow.不幸的是,该守则,以交换整数值将失败的价值观造成过度或底。
However, you can use exclusive-or (xor), which is like add without carry.但是,您可以使用专用或(异或) ,这是想添加未经进行。 The following unobvious code exchanges all int values.以下不明显代码交流的所有诠释的价值观。
x = x ^ y;x = χ ^ y ;y = x ^ y;为Y = χ ^ y ;x = x ^ y;x = χ ^ y ;September 28th, 2006 at 6:10 am 2006年9月28日在上午06时10分
im coding a anagram word game to swap letters from one label to another (up to the down) by using a threeBottons class how should i use the string and char code?即时通讯编码的一大战文字游戏来交换的信件从一个标签到另一个(截至向下)使用threebottons一流的我应该如何使用字符串和烧焦代码? (the second character of label one should place to the second character of label two) i need that much help thanks (第二性格标签之一,应以第二性质的标签2 )我需要很多帮助的感谢
November 29th, 2007 at 8:30 am 2007年11月29日上午8时30分
import java.io.*;进口java.io. * ;
class ab AB类
{ (
public static void main(String arg[]) throws IOException公共静态无效的主要(字符串阿根廷[ ] )全ioexception
{ (
int x=6;诠释x = 6 ;
if(x=6)如果(十= 6 )
{ (
System.out.println(”Hi…”); system.out.println ( “嗨… … ” ) ;
} )
} )
} )
the question is ..问题是.. Is it possible to run this program with out changing if(x=6)是否有可能执行这个程式与列改变,如果(十= 6 )
February 26th, 2008 at 4:34 am 2008年2月26日在上午04时34分
if(x > y)如果(十> y )的
{ (
x = x- y; x =的X - Y ;
y = y + x;为y = y + x年;
x = y - x; x = y -x值;
} )
else其他的
{ (
y = y - x;为y = y -x值;
x = x + y; x =用X + y ;
y = x - y;为Y = x -y ;
} )