如何在不使用第三个变量的情况下在Java中交换两个字符串变量,即临时变量?
String a = "one"
String b = "two"
String temp = null;
temp = a;
a = b;
b = temp;
但这里有第三个变量。我们需要消除第三个变量的使用。
答案 0 :(得分:33)
不使用第三个变量就这样做:
String a = "one";
String b = "two";
a = a + b;
b = a.substring(0, (a.length() - b.length()));
a = a.substring(b.length());
System.out.println("a = " + a);
System.out.println("b = " + b);
答案 1 :(得分:12)
//取自这个答案:https://stackoverflow.com/a/16826296/427413
String returnFirst(String x, String y) {
return x;
}
String a = "one"
String b = "two"
a = returnFirst(b, b = a); // If this is confusing try reading as a=b; b=a;
这是有效的,因为Java语言保证(Java语言规范,Java SE 7版,第15.12.4.2节)所有参数都是从左到右进行评估(与其他语言不同,评估顺序未定义),所以执行顺序是:
b
的原始值,以便作为函数的第一个参数传递b = a
,并将结果(b
的新值)作为函数的第二个参数传递b
的原始值并忽略其新值a
temp
。参数x
的作用为temp
,但它看起来更干净,因为您只需定义一次函数,就可以在任何地方使用它。答案 2 :(得分:6)
String a="one";
String b="two";
a = a.concat("#" + b);
b = a.split("#")[0];
a = a.split("#")[1];
只要您的字符串中不包含#字符,这将起作用。请随意使用任何其他角色。
您可以使用可能的Unicode字符,例如“\ u001E”而不是#。
答案 3 :(得分:2)
public class SwapStringVariable {
public static void main(String[] args) {
// TODO Auto-generated method stub
String a = "test";
String b = "paper";
a = a + b;
b = a.substring(0, a.length() - b.length());
a = a.substring(b.length(), a.length());
System.out.println(a + " " + b);
}
}
答案 4 :(得分:1)
String str1 = "first";
String str2 = "second";
str1 = str2+str1;
str2 = str1.substring(str2.length(),str1.length());
str1 = str1.substring(0,(str1.length() - str2.length()));
System.out.println(str1);
System.out.println(str2);
答案 5 :(得分:0)
我只想指出,以防万一有人通过该线程查看。如果不使用第三个变量,则无法交换两个字符串。在Java示例中,由于字符串是不可变的,因此a = a + b将创建第三个字符串,并且不会重新分配a。执行reverseString,创建一个新变量,但是这次是在程序进入新作用域时在堆栈框架上。可以使用Xor交换,但是在Java中字符串是不可变的,因此Xor交换也将创建一个临时变量。因此,实际上,除非该语言使您能够重新分配给相同的内存空间,否则在不创建新变量的情况下就不可能交换字符串。
答案 6 :(得分:0)
你走了。试试这个:
String a = "one";
String b = "two";
//String temp=null;
int i = a.length();
a += b;
b = a.substring(0, i);
a = a.substring(i, a.length());
System.out.println(a + " " + b);
将变量中的任何值作为字符串。这将是交换。
答案 7 :(得分:0)
您也可以通过使用temp变量来执行此操作,但方式不同:
String a = "one"
String b = "two"
String temp = null;
temp=a.concat(b);
b=temp.substring(0,a.length());
a=temp.substring(a.length(),temp.length());
System.out.println("After Swapping A:"+a+"B:"+b);
答案 8 :(得分:0)
String a = "one";//creates "one" object on heap
String b = "two";// creates "two" object on heap
System.out.printf("a is %s , b is %s%n",a,b);
a = "two";// will not create new "two" object & now a is pointing to "two" object
b = "one";// will not create new "one" object & now b is pointing to "one" object
System.out.printf("a is %s , b is %s%n",a,b);
答案 9 :(得分:0)
String name = "george";
String name2 = "mark";
System.out.println(name+" "+name2);
System.out.println(name.substring(name.length()) + name2 + " "+ name );
这里substring();方法返回空字符串。因此,可以附加名称。
答案 10 :(得分:0)
package com.core;
public class SwappingNoTemp {
public static void main(String[] args) {
String a = "java";
String b = "c";
a = a + b;
b = a.substring(0, a.length() - b.length());
a = a.substring(b.length());
System.out.println("swapping is a.." + a);
System.out.println("swapping is b.." + b);
}
}
答案 11 :(得分:0)
你可以这样做。
public static void main(String[] args) {
// TODO Auto-generated method stub
String a = "one";
String b = "two";
System.out.println(a);
System.out.println(b);
a = a+b;
b = "";
System.out.println("*************");
b = a.substring(0, 3);
a = a.substring(3, 6);
System.out.println(a);
System.out.println(b);
}
答案 12 :(得分:0)
For String 方法1:
public class SwapWithoutThirdVar{
public static void main (String args[]){
String s1 ="one";
String s2 ="two";
s1= s1+s2;
s2 = s1.substring(0,(s1.length()-s2.length()));
s1 = s1.substring(s2.length());
System.out.println("s1 == "+ s1);
System.out.println("s2 == "+ s2);
}
}
方法2:
public class SwapWithoutThirdVar
{
public static void main (String[] args) throws java.lang.Exception
{
String s1 = "one";
String s2 ="two";
s1=s2+s1;
s2=s1.replace(s2,"");
s1=s1.replace(s2,"");
System.out.println("S1 : "+s1);
System.out.println("S2 : "+s2);
}
}
For Integers
公共类SwapWithoutThirdVar {
public static void main(String a[]){
int x = 10;
int y = 20;
x = x+y;
y=x-y;
x=x-y;
System.out.println("After swap:");
System.out.println("x value: "+x);
System.out.println("y value: "+y);
}
}
答案 13 :(得分:0)
a += b;
b = a.replaceFirst(b + "$", "");
a = a.replaceFirst("^" + b, "");
答案 14 :(得分:0)
实际上,你问题中的代码没有交换字符串a,b。
Java正在传递价值: Is Java "pass-by-reference" or "pass-by-value"?
答案 15 :(得分:0)
最简单的方法如下:
String a = "one";
String b = "two";
System.out.println("Before swap: " a + " " + b);
int len = a.length();
a = a + b;
b = a.substring(0, len);
a = a.substring(len);
System.out.println("After swap: " a + " " + b);
答案 16 :(得分:-1)
public class ex{
public static void main(String[] args){
String s1="abc";
String s2="def";
System.out.println(s1);
System.out.println(s2);
s3=s2.replaceAll(s1,s2=s1);
System.out.println(s1);
System.out.println(s2);
}
}
答案 17 :(得分:-1)
对于字符串:
String a = "one"
String b = "two"
a = a + b;
b = a.replace(b, "");
a = a.replace(b, "");