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

时间:2018-04-03 16:34:02

标签: java arrays counting

我们假设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

请注意,我不想计算整个数组中每个项目的出现次数,我只想计算它在连续项目中的出现次数。

7 个答案:

答案 0 :(得分: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;
            }
        }
    }

答案 1 :(得分:2)

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;
            }
        }
    }
}

答案 2 :(得分:1)

您可以使用地图将双精度映射到它出现的次数。然后只需在地图上循环以打印值。

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")
    );
}

这里使用lambdas的一些用途可能被认为更先进,但它们通常会产生最简洁的解决方案。

答案 3 :(得分:1)

我猜所有上述答案都是正确的,但我也想尝试使用单循环,所以这里是单回路:

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);
            }
        }

    }

}
PS:如果有人想让它看起来更清洁,我就可以提出建议。

答案 4 :(得分:0)

如何制作ArrayList<String>

ArrayList<String> runs = new ArrayList<String>();

然后存储类似

的内容
runs.add(current_item + "," + count);

而不是你的印刷线, 然后遍历数组列表并用逗号分隔每个字符串以获得所需的输出。

虽然它可能有用,但它可能并不理想,但仍然是一种可能的解决方案。

编辑:您将在嵌套循环之后进行运行

答案 5 :(得分:0)

以下是更改

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);
}

答案 6 :(得分:0)

我认为这会给出预期的结果

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);
            }
        }