[英]Java reverse string method
我想學習Java。 我需要制作一個稱為reverse的方法,該方法獲取一個字符串並返回一個字符串(但順序相反)。 這是我嘗試過的。 您能修復代碼並解釋我做錯了什么嗎? 還請給我一些有關Java入門的建議。 謝謝!
public class Test{
public static String reverse(String a){
int j = a.length();
char[] newWord = new char[j];
for(int i=0;i<a.length();i++)
{
newWord[j] = a.charAt(i);
j--;
}
return new String(newWord);
}
public static void main(String a[]){
String word = "abcdefgh";
System.out.println(reverse(word));
}
}
您可以使用它來反轉字符串,而無需使用自己的方法
new StringBuilder(word).reverse().toString()
如果要使用解決方案,則必須將int j = a.length()更改為int j = a.length()-1;
固定代碼是
public class Test {
public static String reverse(String a) {
int j = a.length();
char[] newWord = new char[j];
for (int i = 0; i < a.length(); i++) {
newWord[--j] = a.charAt(i);
}
return new String(newWord);
}
public static void main(String a[]) {
String word = "abcdefgh";
System.out.println(reverse(word));
}
}
就像其他人提到的那樣,數組索引從0開始。因此,例如,如果數組的大小為5,則其索引為0、1、2、3、4。 它沒有索引5。
對於長度為5的字符串的示例,我所做的代碼更改為newWord[--j] = a.charAt(i);
將按該順序分配給索引4,3,2,1,0。
關於Java的良好開端,我認為您可以嘗試https://softwareengineering.stackexchange.com/ 。 該網站不適合這種事情。
對於新的Java開發人員來說,這是一個常見的困難。
您缺少的一點是, 數組中的最后一個條目位於a.length-1
位置 。 對於Strings
這是一個改進的版本來演示。
public static String reverse(String a) {
char[] newWord = new char[a.length()];
for (int i = 0, j = a.length() - 1; i < a.length(); i++, j--) {
newWord[j] = a.charAt(i);
}
return new String(newWord);
}
您的方法已經在正確的軌道上。 我要對您說的一件事是,您不需要使用字符數組,而是使用諸如return new String(newWord);
類的東西return new String(newWord);
。 在我看來,這對於初學者Java來說過於復雜。
取而代之的是,您可以創建一個空字符串,並在循環查找要反轉的字符串中的所有字符時,將字符附加在其上。
所以,你for
循環,因為你倒車的話,應該在單詞的末尾開始被逆轉( a
在這種情況下)和向后工作,以索引位置0(即這個詞開始被逆轉)。
嘗試一下,看看這是否對您有意義:
public static String reverse(String a) {
String reversedWord = "";
for (int index = a.length() - 1; index >= 0; index --) {
reversedWord += a.charAt(index);
}
return reversedWord;
}
這一點,那么,在開始的最后a
,在時間(因此使用的工作向后一個字符index --
直到我們到達一個點, index
已經索引位置為0(中間狀態超出了字符index >= 0
)。
在每個索引位置,我們只是從拍攝的字符a
字符串並將其附加到reversedWord
字符串。
希望這可以幫助! 如果您堅持下去,請隨時提出任何其他問題。
在您的for
循環中,主體必須為newWord[--j] = a.charAt(i);
。 這是因為如果數組的長度為8,則其索引為0-7。 因此,我們首先遞減j
,然后將其用作數組指針。
public class Test {
public static String reverse(String a) {
int size= a.length();//size of string
char[] newWord = new char[size];//intialize
for (int i = size-1,j=0; i >=0 ; i--,j++) {//loop start from 7 to 0
newWord[j] = a.charAt(i);// and reverse string goes from 0 to 7
}
return new String(newWord);
}
public static void main(String a[]) {
String word = "abcdefgh";
System.out.println(reverse(word));
}
}
static void reverse {
String word = "Hello World";
StringBuilder str = new StringBuilder(word);
str.reverse();
System.out.println(str);
}
或者你可以做
new StringBuilder(word).reverse().toString()
public static void main(String[] args) {
String input = "hello world";
String output = new String();
for (int i = input.length() - 1; i >= 0; i--) {
output = output + input.charAt(i);
}
System.out.println(output);
}
package Reverse;
import java.util.*;
public class StringReverse {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a String: ");
String original = input.nextLine();
String rev = "";// Initialize as Empty String
for(int i = original.length() - 1 ; i>=0 ; i--){
rev += original.charAt(i);
}
System.out.println("Reverse form: "+rev);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.