//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("--");
}
}
}
주어진 예외 오류가 표시되며 해결 방법을 알 수 없습니다. 그 초보자 질문을 알고, 당신의 대답과 함께 설명이 정말 도움이 될 것입니다.ArrayIndexOutOfBoundsException의 원인은 무엇입니까?
편집 :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 17
at Palindrome.find_palindrome(Palindrome.java:14)
at Palindrome.main(Palindrome.java:8)
배열 길이가 10이면 인덱스가 0에서 9 사이임을 의미합니다. 맞습니까? –
@CodeEnthusiastic 물론입니다, 고마워요. (이 때문에 IMHO 중복됩니다 downvoting). – Maroun
나는 당신의 대답을 편집 한 다음 나의 편집이 사라지고 대답을 수정 한 것을 본다. 인덱스가 '0'에서 '10'까지이면 대답 *이 실패 *하기 때문에 Downvoted. 이제 upvoting. –