Java 计算数组中连续项的出现次数

Java 计算数组中连续项的出现次数,java,arrays,counting,Java,Arrays,Counting,让我们假设项数组由以下项组成{3.1,3.1,3.1,3.2,3.3,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<item

让我们假设
数组由以下项组成
{3.1,3.1,3.1,3.2,3.3,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.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 static void count(List<Double> numbers)
{
    final Map<Double, Integer> numberToOccurrences = new HashMap<>();
    for(Double num : numbers)
    {
        numberToOccurrences.putIfAbsent(num, 0);
        numberToOccurrences.compute(num, (k, occurrences) -> ++occurrences);
    }
    numberToOccurrences.forEach((num, occurrences) -> 
        System.out.println("Number " + num + " occurs " + occurrences + " times")
    );
}
私有静态无效计数(列表编号)
{
final Map NumbertOccessions=新HashMap();
for(双数字:数字)
{
NumbertOccessions.putIfAbsent(num,0);
计算(num,(k,引用)->+引用);
}
NumbertOccessions.forEach((num,引用)->
System.out.println(“数字”+num+“发生”+发生次数+“次数”)
);
}
lambda在这里的一些用途可能被认为是更高级的,但它们通常会产生最简洁的解决方案。

以下是更改

int dec = 0;
int inc = 0;
if(current_item == next_item) {
     count++;
     dec = count; //new line
}
else {
     break;
}
}
//new stuff from here, printing "inc" instead of "count" in print statement.
dec--;
inc++;
if(dec==0){
    System.out.println("item value is " + current_item + " and count is " + inc);
}

您的代码正在迭代先前迭代中已经计算过的值。逻辑中的一个小调整可以按预期工作

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;
            }
        }
    }
private void显示项(列表项){
双电流_项=0;
对于(int i=0;i
应该是公式

public class SuccessiveCounter {
    public static void main(String[] args) throws Exception {
        double[] x = {3.1,3.1,3.1,3.2,3.2,3.1,3.1,3.4,3.4,3.1,3.1};

        for(int n=1,count = 1;n<x.length;n++){
            if(x[n-1]-x[n]==0){
                count++;
            }else{
                System.out.println(x[n]+" "+count);
                count = 1;
            }
        }
    }
}
公共类成功计数器{
公共静态void main(字符串[]args)引发异常{
双[]x={3.1,3.1,3.2,3.2,3.1,3.1,3.4,3.4,3.1,3.1};

对于(int n=1,count=1;n我认为这将给出预期的结果

int count = 1;
        for (int i = 0; i < arr.size(); i++) {
            if (i != 0) {
                if (arr.get(i) - arr.get(i - 1) == 0) {
                    count++;
                } else if (arr.get(i) - arr.get(i - 1) != 0) {
                    System.out.println("Item value is " + arr.get(i - 1) + " and count is " + count);
                    count = 1;
                }
            }
            if (arr.size() == i + 1) {
                System.out.println("Item value is " + arr.get(i) + " and count is " + count);
            }
        }
int count=1;
对于(int i=0;i
我想以上所有答案都是正确的,但我也想尝试单循环,所以这里是单循环:

import java.util.Arrays;
import java.util.List;

public class SuccessiveCount {

    public static void main(String[] args) {
        List<Double> list = Arrays.asList(3.1, 3.1, 3.1, 3.2, 3.2, 3.3, 3.4, 3.4, 3.4, 3.4, 3.1, 3.1);
        double prevValue = list.get(0);
        int count = 0;
        for(int i=0; i < list.size(); i++) {
            if(prevValue == list.get(i)) {
                count++;
            }else {
                System.out.println("item value is "+list.get(i-1)+ " and count is "+ count);
                prevValue = list.get(i);
                count = 1;
            }
            if(list.size() == (i+1)) {
                System.out.println("item value is "+list.get(i-1)+ " and count is "+ count);
            }
        }

    }

}
导入java.util.array;
导入java.util.List;
公共类成功计数{
公共静态void main(字符串[]args){
列表=数组.asList(3.1,3.1,3.1,3.2,3.2,3.3,3.4,3.4,3.4,3.1,3.1);
double prevValue=list.get(0);
整数计数=0;
对于(int i=0;i

PS:如果有人想让它看起来更干净,我会寻求建议。

要计算连续的项目,应该“可能”只有一个循环。我认为你应该循环数组一次,并将项目存储在浮点索引映射中,那么值就是计数。@FallenReapper OP想要计算运行次数,而不是总数。(请参阅预期结果的最后一行。)@user2864740我无法用一个循环完成这不是该问题的重复(“数组中每个项目的Java计数出现次数”)。请查看预期输出。这是一个合适的答案。您不应该对类似的内容进行注释。它从注释开始,然后变成了答案:)这将计算列表中项目的总数
数字3.1出现5次
,但是所需的输出是
3.1=3.2=2 3.3=1 3.4=4 3.1=2
@zawhtut公式:很好)
x[n-1]-x[n]==0 
public class SuccessiveCounter {
    public static void main(String[] args) throws Exception {
        double[] x = {3.1,3.1,3.1,3.2,3.2,3.1,3.1,3.4,3.4,3.1,3.1};

        for(int n=1,count = 1;n<x.length;n++){
            if(x[n-1]-x[n]==0){
                count++;
            }else{
                System.out.println(x[n]+" "+count);
                count = 1;
            }
        }
    }
}
int count = 1;
        for (int i = 0; i < arr.size(); i++) {
            if (i != 0) {
                if (arr.get(i) - arr.get(i - 1) == 0) {
                    count++;
                } else if (arr.get(i) - arr.get(i - 1) != 0) {
                    System.out.println("Item value is " + arr.get(i - 1) + " and count is " + count);
                    count = 1;
                }
            }
            if (arr.size() == i + 1) {
                System.out.println("Item value is " + arr.get(i) + " and count is " + count);
            }
        }
import java.util.Arrays;
import java.util.List;

public class SuccessiveCount {

    public static void main(String[] args) {
        List<Double> list = Arrays.asList(3.1, 3.1, 3.1, 3.2, 3.2, 3.3, 3.4, 3.4, 3.4, 3.4, 3.1, 3.1);
        double prevValue = list.get(0);
        int count = 0;
        for(int i=0; i < list.size(); i++) {
            if(prevValue == list.get(i)) {
                count++;
            }else {
                System.out.println("item value is "+list.get(i-1)+ " and count is "+ count);
                prevValue = list.get(i);
                count = 1;
            }
            if(list.size() == (i+1)) {
                System.out.println("item value is "+list.get(i-1)+ " and count is "+ count);
            }
        }

    }

}