String을 통해 정렬하고 중복을 제거 할 수있는 프로그램을 개발하려고합니다. 이 중첩 된 루프를 사용하고 있습니다. 그러나 내 코드를 실행하면 몇 단어를 반복해서 반복합니다.중첩 된 루프로 중복 제거 java
[ask, null, ask, null, ask, null, ask, null, ask, null, ask, null, ask, null, ask, null, ask]
[not, null, not, null, not, null, not, null, not, null, not, null, not, null, not, null, not]
[what, null, what, null, what, null, what, null, what, null, what, null, what, null, what, null, what]
[your, null, your, null, your, null, your, null, your, null, your, null, your, null, your, null, your]
[country, null, country, null, country, null, country, null, country, null, country, null, country, null, country, null, country]
[can, null, can, null, can, null, can, null, can, null, can, null, can, null, can, null, can]
[do, null, do, null, do, null, do, null, do, null, do, null, do, null, do, null, do]
[for, null, for, null, for, null, for, null, for, null, for, null, for, null, for, null, for]
[you, null, you, null, you, null, you, null, you, null, you, null, you, null, you, null, you]
[ask, null, ask, null, ask, null, ask, null, ask, null, ask, null, ask, null, ask, null, ask]
[what, null, what, null, what, null, what, null, what, null, what, null, what, null, what, null, what]
[you, null, you, null, you, null, you, null, you, null, you, null, you, null, you, null, you]
[can, null, can, null, can, null, can, null, can, null, can, null, can, null, can, null, can]
[do, null, do, null, do, null, do, null, do, null, do, null, do, null, do, null, do]
[for, null, for, null, for, null, for, null, for, null, for, null, for, null, for, null, for]
[your, null, your, null, your, null, your, null, your, null, your, null, your, null, your, null, your]
[country, null, country, null, country, null, country, null, country, null, country, null, country, null, country, null, country]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 17
나는 이것에 대한 넷빈즈를 사용하고 있습니다 :
package q2;
import java.util.Arrays;
public class Q2 {
public static void main(String[] args) {
String sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY";
String lowercaseSentence;
lowercaseSentence = sentence.toLowerCase();
String[] sentenceWords = lowercaseSentence.split(" ");
int LenghtofSentence = sentenceWords.length;
String[] unique = new String[LenghtofSentence];
for (int i = 0; i <= LenghtofSentence; i++) {
//System.out.println(i);
for (int j = 0; j <= LenghtofSentence; j++) {
if (!sentenceWords[i].equals(unique)) {
unique[j] = sentenceWords[i];
j++;
} else {
j++;
}
}
System.out.println(Arrays.toString(unique));
}
}
}
이
내가 무엇입니까 오류 메시지입니다. 어떤 도움을 주셔서 감사합니다. 감사합니다. Keir
코드를 디버그하십시오. – f1sh
'Set'(예 :'LinkedHashSet')을 사용하지 않는 이유는 무엇입니까? – Thomas
'for' 루프를 쓸 때'for (int i = 0; i <= LenghtofSentence; i ++)'배열의 끝을지나 가게됩니다. '<='대신에'<'이어야합니다. – khelwood