英文:
Struggling with substring() logic for 2 character string on CodingBat
问题
以下是翻译的内容:
我已经多次尝试过,但我无法弄清楚CodingBat的解决方案代码如何正确地返回一个两个字符的字符串。
这是他们的解决方案:
public String frontBack(String str) {
if (str.length() <= 1) return str;
String mid = str.substring(1, str.length()-1);
// last + mid + first
return str.charAt(str.length()-1) + mid + str.charAt(0);
}
但是为什么这对于两个字符的字符串起作用呢?如果 str = "AB",那么代码不应该返回 "B"(返回的第一部分)+ "B"(在这种情况下 mid="B")和 "A" ... 所以是 "BBA" 吗?我想我在理解创建一个包含两个字符的字符串的 String mid 方面存在一些逻辑问题。谢谢!
英文:
I've worked through it several times, but I can't figure out how CodingBat's solution code correctly returns a two character string.
This is their solution:
public String frontBack(String str) {
if (str.length() <= 1) return str;
String mid = str.substring(1, str.length()-1);
// last + mid + first
return str.charAt(str.length()-1) + mid + str.charAt(0);
}
But why does this work for two character strings? If str = "AB" shouldn't the code return "B" (first part of return) + "B" (in this case wouldn't mid="B") and "A"...so "BBA"? I think I'm missing some logic with how String mid is created with a 2 character string. Thanks!
答案1
得分: 4
以下是翻译好的内容:
使用 substring
方法更容易,如果你认为字母位于索引之间,就像下面为“AB”所示:
A B
^ ^ ^
0 1 2
而 substring(a,b)
会返回位于索引 a
和 b
之间的字符。
因此,当我们执行以下代码时:
"AB".substring(0,2)
,我们得到完整的"AB"
,"AB".substring(0,1)
,我们得到"A"
。
似乎让你困惑的是当我们执行像这样的代码时:"AB".substring(1,1)
或 (0,0)
或 (2,2)
。
当我们为 start 和 end 索引使用相同的值时,子字符串的结果将是空字符串 ""
,因为在这些索引之间没有字符。
让我们回到你的例子。
当 str = "AB"
并且我们执行
String mid = str.substring(1, str.length()-1);
这与执行 substring(1, 2-1)
相同,即 substring(1,1)
,这意味着 mid
将保存空字符串 ""
。
现在当我们调用
return str.charAt(str.length()-1) + mid + str.charAt(0);
它将被计算为
return 'B' + "" + 'A';
这意味着我们最终会得到
return "BA";
英文:
It is easier to work with substring
method if you think that letters are between indexes like shown below for "AB"
A B
^ ^ ^
0 1 2
and substring(a,b)
would return characters placed between index a
and b
.
So when we execute code like:
"AB".substring(0,2)
we get full"AB"
,"AB".substring(0,1)
we get"A"
What seems to be confusing you is part when we execute code like: "AB".substring(1,1)
OR (0,0)
OR (2,2)
.
When we use same value for start and end index, then result of substring will be empty string ""
because there are no characters between those indexes.
Lets go back to your example.
When str = "AB"
and we execute
String mid = str.substring(1, str.length()-1);
then it is same as executing substring(1, 2-1)
=> substring(1,1)
which means mid
will be holding empty string ""
.
Now when we call
return str.charAt(str.length()-1) + mid + str.charAt(0);
it will be evaluated as
return 'B' + "" + 'A';
which means we will end up with
return "BA";
答案2
得分: 4
我建议安装Eclipse或其他集成开发环境,并以调试模式运行示例。这是学习Java的最佳途径之一。从你的示例中我可以告诉你以下信息:
Mid将为null,因为它从位置1截取到位置1 => 长度 = 0
因此,你将只会看到str.charAt(str.length()-1) + str.charAt(0)作为结果
即B和A。
英文:
I recommend installing Eclipse or Other IDE and run the example in Debug. It's one of the best ways to learn JAVA. From your example I can tell you the following:
Mid will be null because is making a substring from position 1 to 1 => the length = 0
If the substring was from 1 to 2 then it would had showed letter B:
So you will only see as result str.charAt(str.length()-1) + str.charAt(0)
Which is B and A.
答案3
得分: 3
代码交换字符串的第一个和最后一个字符。对于长度为2的字符串,它在“mid”中不存储任何内容,因为“str.substring(1,1)”返回一个空字符串。然后,它返回最后一个字符、存储在“mid”中的空字符串以及第一个字符的连接。
public class MyClass {
public static void main(String args[]) {
String str = "AB";
if (str.length() <= 1) System.out.println(str);
String mid = str.substring(1, str.length()-1);
System.out.println("mid: "+mid);
System.out.println("last: "+str.charAt(str.length()-1));
// last + mid + first
System.out.println(str.charAt(str.length()-1) + mid + str.charAt(0));
}
}
这会打印一个空行,然后是B,然后是BA。
英文:
The code swaps the first and last characters of a string. For a string of length 2, it stores nothing in mid because str.substring(1,1) returns an empty string. Then it returns the concatenation of the last character, the empty string stored in mid and the first character.
public class MyClass {
public static void main(String args[]) {
String str = "AB";
if (str.length() <= 1) System.out.println(str);
String mid = str.substring(1, str.length()-1);
System.out.println("mid: "+mid);
System.out.println("last: "+str.charAt(str.length()-1));
// last + mid + first
System.out.println(str.charAt(str.length()-1) + mid + str.charAt(0));
}
}
This prints an empty line, then B, then BA.
答案4
得分: 3
substring()
的参数是包含/不包含的,因此当索引相同时,substring()
将返回一个空字符串。对于长度为2的字符串,这意味着中值是空的。
以下是以输入为 "AB" 的代码示例:
public String frontBack(String str) { // str = "AB"
// str.length() = 2,继续执行
if (str.length() <= 1) return str;
// str.length()-1 = 1,所以 str.substring(1,1) 得到空字符串。
// mid = "";
String mid = str.substring(1, str.length()-1);
// last + mid + first
// str.charAt(str.length()-1) = "B"
// str.charAt(0) = "A"
// "B" + "" + "A" = "BA"
return str.charAt(str.length()-1) + mid + str.charAt(0);
// 返回 "BA"
}
英文:
The parameters for substring()
is inclusive/exclusive so when the indices are the same, substring()
will return an empty string. With a 2 length string, this means the mid value is empty.
Here's a walkthrough of the code with "AB" as the input
public String frontBack(String str) { // str = "AB"
// str.length() = 2, so continue
if (str.length() <= 1) return str;
// str.length()-1 = 1, so str.substring(1,1) which is empty string.
// mid = "";
String mid = str.substring(1, str.length()-1);
// last + mid + first
// str.charAt(str.length()-1) = "B"
// str.charAt(0) = "A"
// "B" + "" + "A" = "BA"
return str.charAt(str.length()-1) + mid + str.charAt(0);
// returns "BA"
}
答案5
得分: 1
public static String frontBack(String str) {
if (str.length() < 2)
return str;
char first = str.charAt(0);
char last = str.charAt(str.length() - 1);
if (str.length() == 2)
return Character.toString(last) + first;
return last + str.substring(1, str.length() - 1) + first;
}
英文:
public static String frontBack(String str) {
if (str.length() < 2)
return str;
char first = str.charAt(0);
char last = str.charAt(str.length() - 1);
if (str.length() == 2)
return Character.toString(last) + first;
return last + str.substring(1, str.length() - 1) + first;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论