Java Software Programming Examples For Beginners / Interview: How To Swap Integer (and String) Variables Without Using a Temporary Variable Software Java Esempi di programmazione per principianti / Intervista: Come swap intero (e stringa) variabili senza utilizzare un temporaneo variabile
Say x & y are the integer variables. Di 'x & y sono le variabili intero. The challenge is to swap them without using a temporary variable. La sfida è quella di swap di loro senza utilizzare un temporaneo variabile.
The solution is as simple as the problem itself: La soluzione è semplice come il problema stesso:
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. Aggiornamento 1: Jack e Alexey rilevato un errore nel mio soluzione che da allora è stato corretto. Please see their comments below. Si prega di consultare le loro osservazioni qui di seguito.
The simplicity of the solution appeals to me. La semplicità della soluzione appelli a me. It clearly demonstrates the meaning of assignment operator ( “=” ). Esso dimostra chiaramente il significato di assegnazione ( "=").
What if they are Strings? Che cosa se sono stringhe?
Note: You can use String methods in Java API. Nota: È possibile utilizzare metodi String in Java API.
Here is the solution: Ecco la soluzione:
x = x + y; x = x + y;
y = x.substring(0, x.indexOf(y)); x.substring y = (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? Si può vedere la somiglianza?
Can you provide a simpler solution to either of the above? Si può fornire una soluzione più semplice, di una delle suddette?
Update 2: Robert just did (see his comment below). Aggiornamento 2: Robert ha fatto solo (si veda il suo commento qui sotto). He pointed out that the String solution doesn’t work when one string is contained in another. Egli ha sottolineato che la soluzione Stringa non funziona quando una stringa è contenuta in un altro. His solution is: La sua soluzione è:
x = x + y; x = x + y;
y = x.substring(0, x.length() - y.length()); x.substring y = (0, x.length () - y.length ());
x = x.substring(y.length()); x = x.substring (y.length ());
Filed under Elencato sotto How To Come , Java Software Software Java , Tech Note Nota tech | |
| |
RSS 2.0 RSS 2,0 | |
Trackback this Article | questo articolo |
Email this Article Invia questo articolo
You may also like to read Si può anche leggere come |





October 30th, 2005 at 2:53 am 30 ottobre 2005 alle 2:53 am
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 30 ottobre 2005 alle 3:04 am
x = x+y; x = x + y;
y = xy; y = xy;
x = xy; x = xy;
October 30th, 2005 at 8:36 am 30 ottobre 2005 alle 8:36 am
Maybe Forse
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 30 ottobre 2005 alle 9:24 am
@Jack & Alexey @ Jack & Alexey
Oops!
Thanks for the catch. Grazie per la cattura. Corrected. Rettificato.
Just checking if you are attentive Controllo solo se si è attenti
October 30th, 2005 at 9:25 am 30 ottobre 2005 alle 9:25 am
BTW: I actually asked this question today while interviewing candidates. BTW: ho fatto questa domanda oggi, mentre intervistare i candidati.
October 31st, 2005 at 8:15 am 31 ottobre 2005 a 8:15 am
The string sample will fail if the string x contains the string y. La stringa di esempio non andrà a buon fine se la stringa x contiene la stringa y. For example, Per esempio,
String x = “Hello World”; Stringa x = "Ciao Mondo";
String y = “Hello”; Y = stringa "Ciao";
Step 1: x = x + y; Fase 1: x = x + y;
x = “Hello WorldHello” x = "Ciao WorldHello"
Step 2: y = x.substring(0, x.indexOf(y)); Fase 2: y = x.substring (0, x.indexOf (y));
x.indexOf(y) = 0, so y = “”; x.indexOf (y) = 0, quindi y = "";
Step 3: x = x.substring(x.indexOf(y) + y.length()); Fase 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 e y.length = 0, quindi x = "Ciao WorldHello"
You can fix this by using lastIndexOf() method in String like È possibile risolvere questo problema utilizzando lastIndexOf () nel metodo come String
x = x + y; x = x + y;
y = x.substring(0, x.lastIndexOf(y)); x.substring y = (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 o si potrebbero eliminare la indexOf () e calcoli del tutto basta usare le lunghezze delle corde
x = x + y; x = x + y;
y = x.substring(0, x.length() - y.length()); x.substring y = (0, x.length () - y.length ());
x = x.substring(y.length()); x = x.substring (y.length ());
October 31st, 2005 at 8:53 am 31 ottobre 2005 a 8:53 am
@Robert Robert @
I like that. Che mi piace. It looks cleaner. Il suo aspetto più pulito.
November 9th, 2005 at 2:08 am 9 novembre 2005 a 2:08 am
Unfortunately, the code to exchange integer values will fail for values that cause over- or underflow. Purtroppo, il codice per lo scambio di valori interi non andrà a buon fine per i valori che provocano eccessiva o underflow.
However, you can use exclusive-or (xor), which is like add without carry. Tuttavia, è possibile utilizzare esclusiva-o (xor), che è come aggiungere senza trasportare. The following unobvious code exchanges all int values. Unobvious il seguente codice di scambi int tutti i valori.
x = x ^ y;x = x ^ y;y = x ^ y;y = x ^ y;x = x ^ y;x = x ^ y;September 28th, 2006 at 6:10 am Settembre 28, 2006 at 6:10 am
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? nel mese di codifica uno anagramma gioco di parole a lettere di swap da un marchio ad un altro (fino a valle) utilizzando il threeBottons classe Come devo usare la stringa di caratteri e il codice? (the second character of label one should place to the second character of label two) i need that much help thanks (il secondo carattere del marchio si dovrebbe per il secondo carattere del marchio di due) che ho bisogno di molto aiuto, grazie
November 29th, 2007 at 8:30 am 29 novembre 2007 alle 8:30 am
import java.io.*; importazione java.io. *;
class ab classe AB
{ (
public static void main(String arg[]) throws IOException public static void main (String arg []) lancia IOException
{ (
int x=6; int x = 6;
if(x=6) if (x = 6)
{ (
System.out.println(”Hi…”); System.out.println ( "Ciao…");
} )
} )
} )
the question is .. la questione è .. Is it possible to run this program with out changing if(x=6) E 'possibile eseguire il programma con cui cambiano se (x = 6)
February 26th, 2008 at 4:34 am 26 febbraio 2008, 4:34 am
if(x > y) if (x> y)
{ (
x = x- y; x = x-y;
y = y + x; y = y + x;
x = y - x; x = y - x;
} )
else altro
{ (
y = y - x; y = y - x;
x = x + y; x = x + y;
y = x - y; y = x - y;
} )