我知道我的数组有正确的信息(我判断过了),但在我的for循环中,它得到了错误的值.

        int mostFrequent = 0;
        int secondMost = 0;
        for(int i =0;i<256;i++){
            if (counter[i] > mostFrequent) {
                secondMost = mostFrequent;
                mostFrequent = i
            } else if (counter[i] < mostFrequent && counter[i] > secondMost){
                secondMost = i;
            }
        }

我知道我保存整数mostFrequent和secondMost作为数组的索引,这是故意的.我正在比较数组的值,但我想保存这些值的索引.正确的值应该是32和101的指数,但它是不正确的出来.

推荐答案

您正在将价值与指数进行比较! 用counter[i] > counter[mostFrequent]代替counter[i] > mostFrequent

        int mostFrequent = 0;
        int secondMost = 0;
        for(int i =0;i<256;i++){
            if (counter[i] > counter[mostFrequent]) {
                secondMost = mostFrequent;
                mostFrequent = i
            } else if (counter[i] < counter[mostFrequent] && counter[i] > counter[secondMost]){
                secondMost = i;
            }
        }

Java相关问答推荐

Jooq隐式地将bigint转换为数字,并且索引不起作用

neo4j java驱动程序是否会在错误发生时自动回滚事务?

如何配置ActiveMQ Artemis以使用AMQP 1.0和其他协议与Java

有关手动创建的包的问题

在springboot 3中,当我调用api endpoint时,会出现404

测试容器无法加载类路径初始化脚本

如何在Spring Java中从数据库列中获取JSON中的具体数据

GetChildren().emoveAll()不会删除 node

支持MySQL 5.6的最新Hibernate版本

允许同时执行两个方法,但不能同时执行这两个方法

没有使用Lombok生成的参数

Java.lang.invke.LambdaConversionException:实例方法InvokeVirtual的参数数量不正确

我该如何为我的类编写getter和setter方法?

使用@ExceptionHandler的GlobalExceptionHandler还是来自服务器的REST应答的ResponseEntity?

我无法在我的Spring Boot应用程序中导入CSV依赖项

Java System.getProperty在哪里检索user.home?

java.lang.ClassCastException:com.google.firebase.FirebaseException无法转换为com.google.fire base.auth.FirebaseAuthException

如何使用stream.allMatch()为空流返回false?

java.util.LinkedList()是如何成为MutableList的实例的?

Spring Integration SFTP 连接失败 - 无法协商 kex 算法的密钥交换