java - (Bug) 迭代列表后返回错误值
问题描述
这种方法的目的是,当我给它一个字符串作为输入时,它必须返回我最常出现的字母。
如果我给一个字符串作为输入,它可以正常工作:
String text = "helllo";
所以它给了我:
The most frequent letter is l with: 2 occurrences
这是正确的,但如果我把它作为输入
String text = "abbccdd";
它给了我
The most frequent letters are b,d with: 2 occurrences
这不正确,因为它必须给我
The most frequent letters are b,c,d with: 2 occurences
请帮忙!
业务逻辑.java
package com.mycompany.showcharwithhighestoccurrence.javafxBusinessLogic;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class BusinessLogic {
public void hello() {
System.out.println("helllllo");
}
public String giveFinalResult(String text) {
String input = " ";
String output = " ";
if (text != null) {
input = text.toLowerCase().replaceAll("\\s+", "");
int[] freq = new int[input.length()];
int i, j, max, lastFreq;
lastFreq = 0;
//Converts given string into character array
char inputChars[] = input.toCharArray();
List<Character> maxChars = new ArrayList<>();
//Count each word in given string and store in array freq
for (i = 0; i < inputChars.length; i++) {
freq[i] = 1;
for (j = i + 1; j < inputChars.length; j++) {
if (inputChars[i] == inputChars[j] && inputChars[i] != ' ' && inputChars[i] != '0') {
freq[i]++;
inputChars[j] = '0';
}
}
}
//Determine maximum occurring characters
if (freq.length > 0) {
lastFreq = freq[0];
}
for (i = 0; i < freq.length; i++) {
max = freq[0];
if (freq[i] == lastFreq) {
max = lastFreq;
}
if (max == lastFreq && max < freq[i]) {
lastFreq = freq[i];
maxChars.add(inputChars[i]);
for (Character c : maxChars) {
if (c != null) {
output = "The most frequent letter is " + c + " with: " + freq[i] + "occurrences";
}
}
}
if (lastFreq < freq[i]) {
maxChars.clear();
maxChars.add(inputChars[i]);
for (Character c : maxChars) {
if (c != null) {
output = "The most frequent letter is " + c + " with: " + freq[i] + "occurrences";
}
}
}
if (max > 1 && max == freq[i]) {
maxChars.add(inputChars[i]);
output = "The most frequent letter are " + maxChars.get(0).toString() + ", "
+ iterator.next().toString() + " with: " + freq[i] + "occurrences";
}
}
}
}
return output;
}
}
解决方案
为什么这么复杂?
您只需要 3 个简单的步骤:
// collect frequencies
Map<String, Long> collect = "abbccdde".chars()
.mapToObj(c -> Character.toString((char) c))
.collect(Collectors.groupingBy(x -> x, Collectors.counting()));
// find max
long max = collect.values().stream().max(Comparator.naturalOrder()).get();
//filter values for max
String result = collect.entrySet()
.stream()
.filter(x -> x.getValue() == max)
.map(Map.Entry::getKey)
.collect(Collectors.joining(","));
String output = "The most frequent letter are " + result + " with: " + max + "occurrences";
System.out.println("output = " + output);
推荐阅读
- sql - sql server 上的小时计算
- javascript - 如何在输入下显示数据列表,边距顶部
- php - PHP 页面调用 PHP 文件 - 对缓存感到困惑
- c# - 使用带有类的 c# 将数据插入 MS Access
- r - R中DT中的多个表格标题
- azure - 关于将数据上传到 Blob 存储的 Azure 数据工厂管道
- c# - 如何在 JIRA C# 中发送 POST 返回 statusCode 400
- openai-gym - 自定义 OpenAi Gym Taxi v2 环境
- r - 在闪亮的模块中使用 rhandsontable
- kubernetes - 如何探索 Kubernetes 文件夹?