计算数组中连续项的出现 - java

假设items数组由以下项目组成{3.1, 3.1, 3.1, 3.2, 3.2, 3.3, 3.4, 3.4, 3.4, 3.4, 3.1, 3.1}

我想要的是计算连续项目中每个项目的出现,例如:

3.1 = 3 
3.2 = 2
3.3 = 1
3.4 = 4
3.1 = 2

我写了以下函数:

private void displayItems(List<Double> items) {
        double current_item=0;
        for(int i=0; i<items.size(); i++) {
            int count=1;
            current_item = items.get(i);
            if(i != items.size()) {
                for(int j=i+1; j<items.size(); j++) {
                    double next_item = items.get(j);
                    if(current_item == next_item) {
                        count++;
                    }else {
                        break;
                    }
                }
                System.out.println("item value is " + current_item + " and count is " + count);
            }
        }
    }

我得到以下结果:

item value is 3.1 and count is 3
item value is 3.1 and count is 2
item value is 3.1 and count is 1
item value is 3.2 and count is 2
item value is 3.2 and count is 1
item value is 3.3 and count is 1
item value is 3.4 and count is 4
item value is 3.4 and count is 3
item value is 3.4 and count is 2
item value is 3.4 and count is 1
item value is 3.1 and count is 2
item value is 3.1 and count is 1

我该怎么做才能显示如下结果:

item value is 3.1 and count is 3
item value is 3.2 and count is 2
item value is 3.3 and count is 1
item value is 3.4 and count is 4
item value is 3.1 and count is 2

请不要说我不想统计整个数组中每个项目的出现,我只想统计其在连续项目中的出现。

参考方案

您的代码正在迭代先前迭代中已经计算过的值。逻辑上的小调整可以按预期进行。

private void displayItems(List<Double> items) {
        double current_item=0;
        for(int i=0; i<items.size(); i++) {
            int count=1;
            current_item = items.get(i);
            if(i != items.size()) {
                int j=i+1;
                for(; j<items.size(); j++) {
                    double next_item = items.get(j);
                    if(current_item == next_item) {
                        count++;
                    }else {
                        break;
                    }
                }
                System.out.println("item value is " + current_item + " and count is " + count);
                i = j-1;
            }
        }
    }

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

Java:BigInteger,如何通过OutputStream编写它 - java

我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…

Java-如何将此字符串转换为日期? - java

我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…

Java:从类中查找项目名称 - java

仅通过类的实例,如何使用Java反射或类似方法查找项目名称?如果不是,项目名称(我真正想要的是)可以找到程序包名称吗? 参考方案 项目只是IDE使用的简单组织工具,因此项目名称不是类或JVM中包含的信息。要获取软件包,请使用Class#getPackage()。然后,可以调用Package#getName()将包作为您在代码的包声明中看到的String来获取…

JAVA 8具有任何匹配属性的对象的过滤器列表 - java

我的要求是通过匹配任何属性的字符串来过滤对象列表。例如,假设Contact类具有三个属性:街道,城市,电话。我知道java流过滤器是如何工作的,在这里我必须将输入字符串与每个属性进行比较,如下所示:contactList.stream().filter(contact -> contact.getStreet().equals("dubai&…