Я пытаюсь разработать программу, которая может сортировать строку и удалять дубликаты. Для этого я использую вложенные циклы. Однако, когда я запускаю свой код, он повторяет несколько слов снова и снова.Удалить дубликаты с вложенными циклами java
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));
}
}
}
Это сообщение об ошибке я получаю:
[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
Я использую Netbeans для этого. Любая помощь приветствуется. Спасибо Keir
Отлаживайте свой код. – f1sh
Почему бы вам просто не использовать 'Set' (например,' LinkedHashSet')? – Thomas
Когда вы пишете цикл 'for' следующим образом:' for (int i = 0; i <= LenghtofSentence; i ++) 'вы проходите мимо конца вашего массива. Это должно быть '<' вместо '<='. – khelwood