Java实现逗号后字符串反转并输出前半部分的代码疑问
Hey James, I see exactly where you're stuck right now—those single char variables ch1 and ch2 can only hold one character at a time, so they're overwriting each other every loop iteration. That's why you can't access all the elements after the loops finish. Let's fix this using character arrays, which let you store all the characters you need without relying on StringBuilder.
First, let's clarify the requirement: you want to take a string like grdwe,erwd, reverse the part after the comma, then concatenate it with the part before the comma to get dwregrdwe.
Here's how to adjust your code:
- First, find the index of the comma directly (no need to loop through every character to find it—
String.indexOf(',')does this for you). - Initialize two character arrays: one for the reversed post-comma part, and one for the pre-comma part. The size of each array is determined by the length of the corresponding substring.
- Populate the arrays by iterating over the relevant parts of the input string.
- Combine the two arrays into a single result array, then convert it to a string.
Here's the revised code with explanations:
public class ReverseStringAfterComma { public void reverseMethod(String word) { // Find the position of the comma int commaIndex = word.indexOf(','); if (commaIndex == -1) { // Handle case where there's no comma (optional, but good practice) System.out.println(word); return; } // Initialize arrays based on substring lengths char[] reversedPostComma = new char[word.length() - commaIndex - 1]; char[] preComma = new char[commaIndex]; // Populate reversed post-comma array (start from end of the string) for (int i = 0; i < reversedPostComma.length; i++) { reversedPostComma[i] = word.charAt(word.length() - 1 - i); } // Populate pre-comma array (start from beginning up to comma) for (int i = 0; i < preComma.length; i++) { preComma[i] = word.charAt(i); } // Combine the two arrays into one result array char[] result = new char[reversedPostComma.length + preComma.length]; System.arraycopy(reversedPostComma, 0, result, 0, reversedPostComma.length); System.arraycopy(preComma, 0, result, reversedPostComma.length, preComma.length); // Convert result array to string and print System.out.println(new String(result)); } public static void main(String[] args) { ReverseStringAfterComma rsac = new ReverseStringAfterComma(); String str = "grdwe,erwd"; rsac.reverseMethod(str); // Output: dwregrdwe } }
Key fixes and explanations:
- Array Initialization: We create arrays with exact sizes matching the parts we need to store (e.g.,
reversedPostCommahas length equal to the number of characters after the comma). This ensures we can hold every character without overwriting. - Avoiding StringBuider: By using character arrays and
System.arraycopy(an efficient built-in method to copy arrays), we stick to your request of not usingStringBuilder. - Simplified Comma Search:
word.indexOf(',')gives us the position of the comma in one step, making the code cleaner than looping through every character.
If you want to avoid System.arraycopy, you can also loop through both arrays and populate the result array manually, like this:
// Alternative way to combine arrays without System.arraycopy char[] result = new char[reversedPostComma.length + preComma.length]; int index = 0; // Add reversed post-comma characters first for (char c : reversedPostComma) { result[index++] = c; } // Then add pre-comma characters for (char c : preComma) { result[index++] = c; }
Either approach will work perfectly for your use case.
内容的提问来源于stack exchange,提问作者James

