Question:
1) Write code to check a String is palindrome or not?
以下是我一开始写的CODE,后来仔细一看就有很多问题了:
public class CheckStringPalindrome {
String original;
public CheckStringPalindrome(String inputStr) {
this.original = inputStr;
}
private boolean checkStrNotNull() {
if ( this.original == null )
return false;
else
return true;
}
private String reverseStr() {
int length = original.length();
String reverse="";
for( int iCnt=0; iCnt < length; iCnt++ ) {
reverse = reverse + original.charAt(length-iCnt-1);
//这里没注意String is immutable, so here will generate a lot String objects.
//reverse should be a StringBuffer
}
return reverse;
}
private boolean checkStrEquals(String reverse) {
if ( reverse.equals(original)) {
return true;
}
else
return false;
}
public boolean checkStrPalinddrome() {
String reverse = "";
if ( checkStrNotNull() ) {
reverse = reverseStr();
if ( checkStrEquals(reverse)) {
System.out.println("the string is palindrome!");
return true;
}
else {
System.out.println("the string is not palindrome!");
return false;
}
}
else
return false;
}
public static void main(String argv[]) {
String inputString = "AAABBBBBBAAA";
CheckStringPalindrome csp = new CheckStringPalindrome(inputString);
csp.checkStrPalinddrome();
}
}
Fix version 1:
private String reverseStr() {
int length = original.length();
StringBuilder reverse= new StringBuilder(length);
for( int iCnt=0; iCnt < length; iCnt++ ) {
reverse.append(original.charAt(length-iCnt-1));
}
return reverse.toString();
}
问题这里是:stringBuffer has a reverse method.
fix 2:
private String reverseStr() {
StringBuilder temp = new StringBuilder(original);
return temp.reverse().toString();
}
Don't use a String if you want something mutable. Instead you're better off
using a StringBuilder object. Then when you're done modifying it, you can
call toString() on it to get the String you need. The API will show you the
methods of this flexible class.
String is immutable class. Here is another solution : convert to StringBuffer
or StringBuilder and delete from the startIndex to endIndex
String s = "test tes te t ";
String str = new StringBuffer(s).delete(start, end).toString();
for example :
String str = new StringBuffer(s).delete(0, 8).toString();
System.out.println(str); // s te t
1) Write code to check a String is palindrome or not?
以下是我一开始写的CODE,后来仔细一看就有很多问题了:
public class CheckStringPalindrome {
String original;
public CheckStringPalindrome(String inputStr) {
this.original = inputStr;
}
private boolean checkStrNotNull() {
if ( this.original == null )
return false;
else
return true;
}
private String reverseStr() {
int length = original.length();
String reverse="";
for( int iCnt=0; iCnt < length; iCnt++ ) {
reverse = reverse + original.charAt(length-iCnt-1);
//这里没注意String is immutable, so here will generate a lot String objects.
//reverse should be a StringBuffer
}
return reverse;
}
private boolean checkStrEquals(String reverse) {
if ( reverse.equals(original)) {
return true;
}
else
return false;
}
public boolean checkStrPalinddrome() {
String reverse = "";
if ( checkStrNotNull() ) {
reverse = reverseStr();
if ( checkStrEquals(reverse)) {
System.out.println("the string is palindrome!");
return true;
}
else {
System.out.println("the string is not palindrome!");
return false;
}
}
else
return false;
}
public static void main(String argv[]) {
String inputString = "AAABBBBBBAAA";
CheckStringPalindrome csp = new CheckStringPalindrome(inputString);
csp.checkStrPalinddrome();
}
}
Fix version 1:
private String reverseStr() {
int length = original.length();
StringBuilder reverse= new StringBuilder(length);
for( int iCnt=0; iCnt < length; iCnt++ ) {
reverse.append(original.charAt(length-iCnt-1));
}
return reverse.toString();
}
问题这里是:stringBuffer has a reverse method.
fix 2:
private String reverseStr() {
StringBuilder temp = new StringBuilder(original);
return temp.reverse().toString();
}
Don't use a String if you want something mutable. Instead you're better off
using a StringBuilder object. Then when you're done modifying it, you can
call toString() on it to get the String you need. The API will show you the
methods of this flexible class.
String is immutable class. Here is another solution : convert to StringBuffer
or StringBuilder and delete from the startIndex to endIndex
String s = "test tes te t ";
String str = new StringBuffer(s).delete(start, end).toString();
for example :
String str = new StringBuffer(s).delete(0, 8).toString();
System.out.println(str); // s te t