简体   繁体   English

如何使用 Stringbuilder 和 for 循环反转字符串

[英]How to reverse a String with Stringbuilder and for loop

I managed to reverse a String making it a char array, also with .reverse method on a Stringbuilder, managed to do it with another String and a for loop (I read that this isn't a good idea because it hurts the performance), but i cant figure out how to reverse a string with a Stringbuilder and for loop.我设法反转一个 String 使其成为一个字符数组,也在 Stringbuilder 上使用 .reverse 方法,设法用另一个 String 和一个 for 循环来做到这一点(我读到这不是一个好主意,因为它会损害性能),但我不知道如何使用 Stringbuilder 和 for 循环反转字符串。 It gives me error - "The left-hand side must be a variable" .它给了我错误 - “左侧必须是一个变量”。 Also tried to set a length for the Stringbuilder, but looks like I am missing something.还尝试为 Stringbuilder 设置长度,但看起来我遗漏了一些东西。 I tried to set indexes outside the brackets of the Stringbuilder, because they are for parameters, but I am just guessing how to do it, tried .append as well.我试图在 Stringbuilder 的括号外设置索引,因为它们用于参数,但我只是在猜测如何做,也尝试了 .append 。

public class Task1 {

    public static void main(String[] args) {

        String str = "Reverse me";
        StringBuilder printStr = new StringBuilder();
        int j = 0;

        for (int i = str.length()-1; i > 0; i--) {
            printstr(j) = str[i];

        }
    }

}

I think you are on the right lines, just a few things I'd change: Firstly we have the ability to specify the length of the StringBuilder because we know it is going to be the same length as the String we are reversing.我认为您在正确的路线上,只是我要更改的几件事:首先,我们能够指定StringBuilder的长度,因为我们知道它与我们正在反转的String长度相同。

It is not necessary to loop through the entire array, just half the array will suffice performing operations which are essentially just switching the characters.没有必要遍历整个数组,只需一半的数组就足以执行本质上只是切换字符的操作。 So the right-most character is now the left-most character, this saves a little computation.所以最右边的字符现在是最左边的字符,这节省了一点计算。

public static void main(String[] args) throws IOException {
    String str = "Reverse me";
    StringBuilder printStr = new StringBuilder();
    printStr.setLength(str.length());

    for(int i =0; i<str.length()/2;i++){
        char left = str.charAt(i);
        final int fromRight = str.length() - i - 1;
        printStr.insert(i, str.charAt(fromRight));
        printStr.insert(fromRight, left);
    }
}

Here is a visual representation of how to character switching essentially works: More information can be found here以下是角色切换本质上如何工作的直观表示:可以在此处找到更多信息

在此处输入图片说明

OK, so you made multiple mistakes, you are trying to access indexes by using parenthesis, you are not iterating over whole string as your loop condition is i > 0 and it should be i >= 0, and your loop is just wrong (disregard parenthesis), you can combine append() and charAt() methods to do what you need:好的,所以你犯了多个错误,你试图通过使用括号访问索引,你没有迭代整个字符串,因为你的循环条件是 i > 0,它应该是 i >= 0,你的循环是错误的(忽略括号),你可以结合 append() 和 charAt() 方法来做你需要的:

    for (int i = str.length()-1; i >= 0; i--) {
        printStr.append(str.charAt(i));
    }

We go from last index of your string up to index 0, so we start from the end of the string, and we append each character to our StringBuilder.我们从字符串的最后一个索引到索引 0,所以我们从字符串的末尾开始,并将每个字符附加到我们的 StringBuilder。 In the end you have your reversed string.最后你有你的反向字符串。

    public class HelloWorld {
    public static void main(String[] args) {

        System.out.println(reverseString("Example"));
    }

    public static String reverseString(String str) {
        StringBuilder builder = new StringBuilder();

        for (int i = str.length() - 1; i >= 0; i--) {
            builder.append(str.charAt(i));
        }
        return builder.toString();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM