//Palindrone from a String
public class Palindrome {
static int track = 0;
public static void main(String args[]){
String str = "abcicbbcdefggfed ";
char[] charArray = str.toCharArray();
Palindrome p1 = new Palindrome();
p1.find_palindrome(charArray);
}
void find_palindrome(char[] ch){
for(int i=0; i< ch.length; i++){
if(ch[i] == ch[i+1]){
checkPalindrome(ch, i, i+1);
}
else{
checkPalindrome(ch,i-1,i+1);
}
}
}
void checkPalindrome(char[] c, int left, int right){
int count=0,l=0,r=0;
while(left >= 0 && right <= c.length){
while(c[left] == c[right]){
left--;
right++;
count = count + 1;
}
break;
}
if(count > track){
track = count;
l = left;
r = right;
}
if(count>1){
for (int j=left+1;j<=right-1;j++){
System.out.println(c[j]);
}
System.out.println("--");
}
}
}
我看到给定的异常错误,我不知道如何解决。我知道它的初学者问题,所以解释和你的答案真的会有所帮助。
编辑:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 17
at Palindrome.find_palindrome(Palindrome.java:14)
at Palindrome.main(Palindrome.java:8)