如何在不使用第三个变量(即 temp 变量)的情况下在 Java 中交换两个字符串变量?
String a = "one"
String b = "two"
String temp = null;
temp = a;
a = b;
b = temp;
但是这里还有第三个变量。我们需要去掉第三个变量的使用。
最佳答案
在不使用第三个变量的情况下这样做:
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);
关于java - 如何在不使用第三个变量的情况下在 Java 中交换两个字符串变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18528697/