java - 阵列频率

标签 java arrays

我正在尝试用 50 个整数值初始化一个数组,并计算 10 .. 19 范围内数字的频率。 。我认为问题出在代码的底部。

import java.util.Scanner;
public class Ex1partA {
  public static void main(String[] args) {
    Scanner kbin = new Scanner(System.in);

    int list[]=new int[50];     
    int i=0;

    System.out.print("\n\tInput numbers from 10 to 19: \n");

    while (i < 50) {
      int value = kbin.nextInt();
      if (value >= 10 & value <= 19) {
        list[i] = value;
        i++;
      } else {
        System.out.println("!! Bad number !!");
      }
    }

    for (int value : list) {
      System.out.println("" + value);
    }
  }
}

最佳答案

I'm trying to initialize an array with 50 integer values and compute the frequency of numbers in the range of 10 .. 19.

要计算频率,最好将所有数字放入 List 中,并使用 Collections.Frequency 计算频率

  List<Integer> freqList=new ArrayList<Integer>();
   // Add numebers to this list.

    for (int i = 10; i <20; i++) {
      int freq=Collections.frequency(freqList, i);
                      // This will return frequency of number

        System.out.println(i+"  "+freq);  
    }

关于java - 阵列频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20198658/

相关文章:

java - 使用 maven+netbeans 定位旧的 java 版本而不安装旧的 java 版本?

java - 如何让本地机器承担IAM角色

java - 如何打包 JUnit 测试

java - 检查两个数组android之间的相同值

c - 尝试将结构体中的指针分配给二维数组

java - apache tomcat 6.0.20 中的 Servlet 映射

java - Java 语言规范示例 8.1.2-1(相互递归类型变量边界)

javascript - 按键值过滤 JSON

php - 来自 php mysql 的数组值的总和

java - 零长度多维数组