Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按猿的数量排序_Java_Bufferedreader_Bufferedwriter - Fatal编程技术网

Java 按猿的数量排序

Java 按猿的数量排序,java,bufferedreader,bufferedwriter,Java,Bufferedreader,Bufferedwriter,例如,给我一个单词,我必须按照该单词中出现的次数对其字母进行排序,如果两个字母出现的次数相同,则将按照字典最小值对其进行排序。 现在,我已经开始了解一个字母在一个单词中出现了多少次,但从这里我不知道确切的方法。 这个问题要求我使用BufferedReader和BufferedWriter import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Sc

例如,给我一个单词,我必须按照该单词中出现的次数对其字母进行排序,如果两个字母出现的次数相同,则将按照字典最小值对其进行排序。 现在,我已经开始了解一个字母在一个单词中出现了多少次,但从这里我不知道确切的方法。 这个问题要求我使用BufferedReader和BufferedWriter

import java.util.*;

public class Main {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Map<Character, Integer> m = new HashMap<>();
    String s = sc.nextLine();
    for (int i = 0; i < s.length(); ++i) {
        char c = s.charAt(i);
        if (m.containsKey(c))
            m.put(c, m.get(c) + 1);
        else
            m.put(c, 1);
    }
    for (char letter = 'a'; letter <= 'z'; ++letter)
        if (m.containsKey(letter))
            System.out.println(letter + ": " + m.get(letter));
}
import java.util.*;
公共班机{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
Map m=新的HashMap();
字符串s=sc.nextLine();
对于(int i=0;i对于(char letter='a';letter来计算word中的字母,您可以使用更简单的方法:

用26个零定义数组,扫描输入行并增加该数组中的适当索引,因此,如果遇到“a”(或“a”-是相同的字母,但符号不同)-您将增加索引0处的值,b-索引1,等等

在此扫描期间,您还可以计算最常出现的符号,如下所示:

public static void main(final String[] args) throws IOException {
    char maxSymbol = 0;
    int maxCount = 0;

    final int[] counts = new int[26]; // number of times each letter (a-z) appears in string
    try (final BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
        final String s = br.readLine().toLowerCase(); // calculate case-insensitive counts
        for (final char c : s.toCharArray()) {
            final int idx = c - 'a'; // convert form ASCII code to 0-based index
            counts[idx]++;

            if (counts[idx] > maxCount) {
                maxSymbol = c; // we found most occurred symbol for the current moment
                maxCount = counts[idx];
            } else if (counts[idx] == maxCount) { // we found 2nd most occurred symbol for the current moment, need to check which one is minimal in lexicographical order
                if (c < maxSymbol) {
                    maxSymbol = c;
                }
            }
        }
    }

    if (maxSymbol > 0) {
        System.out.println("Most frequent symbol " + maxSymbol + " occurred " + maxCount);
    }
}
publicstaticvoidmain(最终字符串[]args)引发IOException{
char maxSymbol=0;
int maxCount=0;
final int[]counts=new int[26];//每个字母(a-z)在字符串中出现的次数
try(final BufferedReader br=new BufferedReader(new InputStreamReader(System.in))){
最后一个字符串s=br.readLine().toLowerCase();//计算不区分大小写的计数
for(最终字符c:s.toCharArray()){
final int idx=c-'a';//将格式ASCII代码转换为基于0的索引
计数[idx]++;
if(计数[idx]>maxCount){
maxSymbol=c;//我们找到了当前时刻出现最多的符号
maxCount=计数[idx];
}否则,如果(counts[idx]==maxCount){//我们找到了当前时刻出现次数第二多的符号,则需要检查哪一个符号按字典顺序最小
if(c0){
System.out.println(“最频繁的符号”+maxSymbol+“发生”+maxCount);
}
}

我用缓冲读卡器从stdin获取数据,但我不知道将缓冲写卡器放在哪里,也许是为了打印结果?

我希望这就是您想要的

public static void main(String[] args) {
    Map<Character, Integer> m = new HashMap<>();
    String testString = "Instructions";
    Map<Character, List<Character>> map = new HashMap<>();

    for (int i = 0; i < testString.length(); i++) {
        char someChar = testString.charAt(i);
        if (someChar == ' ') {
            continue;
        }
        char ch = testString.charAt(i);
        List<Character> characters = map.getOrDefault(Character.toLowerCase(ch), new ArrayList<>());
        characters.add(ch);
        map.put(Character.toLowerCase(ch), characters);
    }
    List<Map.Entry<Character, List<Character>>> list = new ArrayList<>(map.entrySet());

    list.sort((o1, o2) -> {
        if (o1.getValue().size() == o2.getValue().size()) {
            return o1.getKey() - o2.getKey();/// your lexicographic comparing
        }
        return o2.getValue().size() - o1.getValue().size();
    });

    list.forEach(entry -> entry.getValue().forEach(System.out::print));
}
publicstaticvoidmain(字符串[]args){
Map m=新的HashMap();
String testString=“指令”;
Map Map=newhashmap();
对于(int i=0;i{
如果(o1.getValue().size()==o2.getValue().size()){
返回o1.getKey()-o2.getKey();///您的字典
}
返回o2.getValue().size()-o1.getValue().size();
});
list.forEach(entry->entry.getValue().forEach(System.out::print));
}

使用缓冲编写器有什么真正的原因吗?在问题中添加一些示例,即输入->输出。这样你可以更好地澄清内容。相关/相同的过程/问题:(同一个人:?相同的代码??)这没关系,但在问题结束时,这个问题对我有帮助。例如,我有作为输入:指令;作为输出,它必须显示:iInNotCuoss。也就是说,字母按频率和字母顺序排序,以防两个字母出现相同的次数。为您修改)“prog.java:23:error:')”预期的list.sort((o1,o2)->{”,好的,当我想测试这个问题时,我得到了这个错误,可能来自Java版本或idea?使用sdk 8或替换lambda来匿名类