给定一个决定单词输出、每个单词的长度和单词重复次数的问题,我有以下代码能够确定下面每个单词的单词和长度:
String sentence;
String charSentence;
String[] wordOutput;
private void analyzeWords(String s) {
String[] words = sentence.split(" ");
wordOutput = new String[words.length];
int[] repeats = new int[words.length];
// Increment a single repeat
for (int i = 0; i < words.length; i++) {
repeats[i] = 1;
// Increment when repeated.
for (int j = i + 1; j < words.length - 1; j++) {
if (words[i].equalsIgnoreCase(words[j])) {
repeats[i]++;
}
}
wordOutput[i] = words[i] + "t" + words[i].length() + "t" + repeats[i];
}
当我运行程序时,我得到以下输出:
Equal 5 2
Equal 5 1 <- This is a duplicate word and should not be here when it repeats.
有人知道我的问题在哪里吗?是不是与我的重复数组有关?
第一个问题是,在内部for循环中,您正在从i+1
循环到length-1
。你需要循环到length
。其次,您需要确定该单词是否在String
中出现,如果出现,请使用continue
语句。你可以做:
outer:
for (int i = 0; i < words.length; i++) {
repeats[i] = 1;
for(int index = i-1; index >= 0; index--) {
if(words[i].equals(words[index])) {
continue outer;
}
}
...
}
然而,这样做的问题是,当您指定一个长度与字数相同的Array
时,列表末尾会有null
值。要解决这个问题,你可以做:
wordOutput = Arrays.stream(wordOutput).filter(e-> e!= null).toArray(String[]::new);
它将过滤掉null
值
输出:
(输入String
:"This is a String is a with a lot lot of this repeats repeats"
(
This 4 2
is 2 2
a 1 3
String 6 1
with 4 1
lot 3 2
of 2 1
this 4 1
repeats 7 2
不是在所有索引中递增计数,而是只在单词的最后一次出现时存储计数,在其他情况下,计数值为0。最后遍历计数数组,如果它大于零,则打印值及其计数
private void analyzeWords(String s) {
String[] words = sentence.split(" ");
wordOutput = new String[words.length];
int[] repeats = new int[words.length];
for (int i = 0; i < words.length; i++) {
int count =1;
int index = i;
for (int j = i + 1; j < words.length - 1; j++) {
if (words[i].equalsIgnoreCase(words[j])) {
count++;
index = j;
}
}
if(repeats[index]==0){
repeats[index]=count; // update repeat array only for last occurence of word
wordOutput[i] = words[i] + "t" + words[i].length() + "t" + repeats[index];
}
}
首先,正如GBlodgett提到的,您应该检查所有剩下的单词是否重复,您当前的解决方案跳过最后一个单词。将第二个循环终止条件更新为j < words.length
。
其次,如果只想打印一次重复项,则需要在解决方案中设置一个条件。其中一个例子:
boolean[] duplicates = new boolean[words.length];
// Increment a single repeat
for (int i = 0; i < words.length; i++) {
repeats[i] = 1;
// Check for duplicates,
// If the word was not marked as duplicate
if (!duplicates[i]) {
// Increment when repeated.
for (int j = i + 1; j < words.length; j++) {
if (words[i].equalsIgnoreCase(words[j])) {
repeats[i]++;
duplicates[j] = true;
}
}
wordOutput[i] = words[i] + "t" + words[i].length() + "t" + repeats[i];
}
}
有一个Java 8+解决方案,例如:
Map<String, Long> m = Arrays.stream(s.split(" ")).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
地图将出现成对的单词和it。