【问题标题】:Count the occurrences of successive items in an array计算数组中连续项的出现次数
【发布时间】:2018-04-03 16:34:02
【问题描述】:

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

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

【问题讨论】:

  • 要计算连续项目,应该“可能”只有一个循环。
  • 我认为您应该遍历数组一次,并将项目存储在浮点索引映射中,然后该值就是计数。
  • @Fallenreaper OP 想要计算运行次数,而不是总数。 (见预期结果的最后一行。)
  • @user2864740 我不能用一个循环来做到这一点
  • 这不是与 that 问题(“Java 计算数组中每个项目的出现次数”)的重复。查看预期输出。

标签: java arrays counting


【解决方案1】:

您的代码正在迭代先前迭代中已计算的值。逻辑上的小调整按预期工作。

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

【讨论】:

    【解决方案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;
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用映射将双精度映射到它出现的次数。然后简单地遍历地图以打印值。

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

      这里的一些 lambda 用法可能被认为更高级,但它们通常会产生最简洁的解决方案。

      【讨论】:

      • 这将计算列表中的项目总数 Number 3.1 occurs 5 times 但是所需的输出是 3.1 = 3 3.2 = 2 3.3 = 1 3.4 = 4 3.1 = 2
      【解决方案4】:

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

      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 : 如果有人想让它看起来更干净,我很乐意提出建议。

      【讨论】:

      • @zawhtut 很好地接触公式 :)
      【解决方案5】:

      创建一个ArrayList&lt;String&gt; 怎么样?

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

      然后存储类似的东西

      runs.add(current_item + "," + count);
      

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

      虽然它可能会起作用,但它可能并不理想,但仍然是一个可能的解决方案。

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

      【讨论】:

      • 这是一个合适的答案。你不应该评论这样的事情。
      • 一开始是评论然后变成了答案:)
      【解决方案6】:

      这里是变化,

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

      【讨论】:

        【解决方案7】:

        我认为这会产生预期的结果

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-12
          相关资源
          最近更新 更多