使用嵌套循环删除重复项 java
Remove duplicates with nested loops 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。任何帮助表示赞赏。
谢谢
基尔
我不知道你为什么要为此使用 for 循环并使其变得复杂。
只需使用 java 中的 Set
即可完成。 Set
是一个不包含重复元素的集合。更多link
Set<String> mySet = new LinkedHashSet<String>(Arrays.asList(sentenceWords));
这将自动删除重复项。您可以按如下方式从 Set
取回不重复的数组:
String[] unique = myset.toArray(new String[myset.size()]);
在使用上面的代码之前还要导入以下内容:
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
使用LinkedHashSet
将保持单词在数组中的顺序。希望对你有帮助。
考虑到你的问题似乎是一个练习,我认为我们不应该给你一个解决方案,而是一个解释如何找到解决方案的建议。首先,如果你可以在你的练习中使用 Java 集合,使用 Set<String>
会给你带来改进,因为它会检查一个单词是否重复并给你一个没有重复的集合。
如果在练习中只能使用数组,则必须使用不同的解决方案。
我建议首先迭代 unique
数组以检查是否有重复项,然后对 unique
数组进行排序。
另一方面,Netbeans 允许您逐步执行代码(正如@f1sh 所建议的)。
package test;
import java.util.ArrayList;
import java.util.Arrays;
public class Test {
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[] uniqueString = new String[LenghtofSentence];
ArrayList<String> unique = new ArrayList<String>();
int k=0;
for(int i=0;i<LenghtofSentence;i++)
{
if(!unique.contains(sentenceWords[i]))
{
unique.add(sentenceWords[i]);
k++;
}
}
for(int i=0;i<unique.size();i++)
{
uniqueString[i] = unique.get(i);
System.out.print(" "+uniqueString[i]);
}
}
}
首先,你的逻辑看起来并不完美。
ArrayIndexOutOfBound
是因为数组索引错误 unique
& sentenceWords
在两个循环中用 <
替换 <=
因为数组的长度是数组中元素的数量&索引从0开始。
正如我之前所说,您应该重新考虑一下您的逻辑,因为它并不完美。您可以使用以下技巧来实现您的目标。
将以下代码替换为您的代码以删除重复项并对数组进行排序..
String[] unique = Arrays.stream(sentenceWords)
.distinct().sorted().toArray(String[]::new);
执行此语句后,数组 unique
包含数组 sentenceWords
的不同元素,这些元素已按词法顺序排序。有关详细信息,请参阅 javadocs。
我正在尝试开发一个可以对字符串进行排序并删除重复项的程序。我为此使用嵌套循环。然而,当我 运行 我的代码时,它只是一遍又一遍地重复几个单词。
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。任何帮助表示赞赏。 谢谢 基尔
我不知道你为什么要为此使用 for 循环并使其变得复杂。
只需使用 java 中的 Set
即可完成。 Set
是一个不包含重复元素的集合。更多link
Set<String> mySet = new LinkedHashSet<String>(Arrays.asList(sentenceWords));
这将自动删除重复项。您可以按如下方式从 Set
取回不重复的数组:
String[] unique = myset.toArray(new String[myset.size()]);
在使用上面的代码之前还要导入以下内容:
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
使用LinkedHashSet
将保持单词在数组中的顺序。希望对你有帮助。
考虑到你的问题似乎是一个练习,我认为我们不应该给你一个解决方案,而是一个解释如何找到解决方案的建议。首先,如果你可以在你的练习中使用 Java 集合,使用 Set<String>
会给你带来改进,因为它会检查一个单词是否重复并给你一个没有重复的集合。
如果在练习中只能使用数组,则必须使用不同的解决方案。
我建议首先迭代 unique
数组以检查是否有重复项,然后对 unique
数组进行排序。
另一方面,Netbeans 允许您逐步执行代码(正如@f1sh 所建议的)。
package test;
import java.util.ArrayList;
import java.util.Arrays;
public class Test {
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[] uniqueString = new String[LenghtofSentence];
ArrayList<String> unique = new ArrayList<String>();
int k=0;
for(int i=0;i<LenghtofSentence;i++)
{
if(!unique.contains(sentenceWords[i]))
{
unique.add(sentenceWords[i]);
k++;
}
}
for(int i=0;i<unique.size();i++)
{
uniqueString[i] = unique.get(i);
System.out.print(" "+uniqueString[i]);
}
}
}
首先,你的逻辑看起来并不完美。
ArrayIndexOutOfBound
是因为数组索引错误 unique
& sentenceWords
在两个循环中用 <
替换 <=
因为数组的长度是数组中元素的数量&索引从0开始。
正如我之前所说,您应该重新考虑一下您的逻辑,因为它并不完美。您可以使用以下技巧来实现您的目标。
将以下代码替换为您的代码以删除重复项并对数组进行排序..
String[] unique = Arrays.stream(sentenceWords)
.distinct().sorted().toArray(String[]::new);
执行此语句后,数组 unique
包含数组 sentenceWords
的不同元素,这些元素已按词法顺序排序。有关详细信息,请参阅 javadocs。