1.寫出一個反轉字串的方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static String reverse(String str) | |
{ | |
char[] charArray=str.toCharArray(); | |
str=""; | |
for(char element:charArray) | |
{ | |
str=element+str; | |
} | |
return str; | |
} |
2.一樣是反轉字串的方法,但不行使用迴圈的方式
這部分我分兩個...一個是我自己寫的,一個是看網路上比較簡易的==
2.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static String reverse(String str, int current) | |
{ | |
current++; | |
if(current>str.length()) | |
{ | |
return ""; | |
} | |
else | |
{ | |
return reverse(str,current)+str.charAt(current-1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static String reverse(String str){ | |
if(str.equals("")){ | |
return ""; | |
} | |
else{ | |
return reverse(str.substring(1))+str.substring(0,1); | |
} | |
} |