计算Java数组中整数的出现次数 [英] Counting occurences of integers in a java array

查看:94
本文介绍了计算Java数组中整数的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算数组中整数的出现次数。我希望能够修改以下计数字符的循环...

  for(int i = 0; i< ; chars.length; i ++)
counts [chars [i]-'a'] ++;

我试图像这样修改它,但收到ArrayIndexOutOfBoundsException错误...

 为(int i = 0; i< ints.length; i ++)
counts [ints [i]-'0'] ++ ;

有什么想法吗?



我已经搜索过



更新



这是我更新的代码。用户输入从1到25的整数。该程序应该使用另一个数组对这些整数的出现进行计数。我使用...

  counts [vals [i]-1] ++;更新了代码。 

哪个允许程序进行编译。但是,count的所有值均为零。



更新代码...

  import java.util.Scanner ; 


class CountIntOccurs
{
private static final int MAX_NUM_OF_VALUES = 200;

public static void main(String [] args)
{
Scanner in = new Scanner(System.in);
int [] vals = new int [MAX_NUM_OF_VALUES];
int numOfVals = 0;

//读取并存储值
System.out.println(输入整数< = 25,每行输入一个,完成时按Control-Z:);
而(in.hasNextLine())
{
vals [numOfVals] = Integer.parseInt(in.nextLine());
++ numOfVals;
}

//如果没有输入值,则关闭应用程序
if(numOfVals == 0)
System.exit(0);


//声明计数数组
int [] counts = new int [25];


System.out.println(counts.length);

// ???????对于数组中的每个整数,将其计数为
,以用于(int i = 0; i< counts.length; i ++){
counts [vals [i]-1] ++;
}



//为(int i = 0; i< numOfVals; ++ i)
显示显示的值
{
System.out.println(vals [i]);
}


// ??????显示计数的整数???
for(int i = 0; i< counts.length; ++ i)
{
System.out.println(counts [i]);
}


}
}



<现在,我需要显示输入的整数的出现。如果我输入1 1 2 2 2,则结果应为2 3 0 0 0 ... 0。

解决方案

  for(int i = 0; i< counts.length; i ++){
counts [vals [i] -1] ++;
}

这是问题所在,因为您有个计数。长度,而不是 numOfVals 。对于每个输入的值,该循环应该使某个数字的计数增加1,因此您必须继续循环直到对每个输入的值进行计数。



如果使用 counts.length ,如果 numOfVals <,则会得到 ArrayIndexOutOfBoundsException 。 / code>小于 25 (因为 counts.length 初始化为 25 )。如果 numOfVals 大于 25 ,则仅计算前25个输入。


因此,

解决方案是:

 为(int i = 0; i  counts [vals [i] -1] ++; 
}

现在,您正在使用<$ c在新行上打印每个数字$ c> println()而不是 print(),并且您没有打印任何文本,因此很难解释输出该程序。我对该程序进行了一些肤浅的更改,以使其更易于理解。这是完整的程序:

  public static void main(String [] args){
Scanner in = new Scanner( System.in);
int [] vals = new int [MAX_NUM_OF_VALUES];
int numOfVals = 0;

System.out.println(输入整数< = 25,每行输入一个,完成后按control-Z:);
而(in.hasNextLine()){
vals [numOfVals] = Integer.parseInt(in.nextLine());
++ numOfVals;
}
if(numOfVals == 0)
System.exit(0);

int []个计数=新int [25];

//将counts.length更改为numOfVals
for(int i = 0; i< numOfVals; i ++){
counts [vals [i] -1] ++ ;
}

//显示输入的值
System.out.print(输入的值:);
for(int i = 0; i< numOfVals; ++ i){
System.out.print(vals [i]);
}
System.out.println(); //新行

//显示计数的整数
System.out.print(值的计数:);
for(int i = 0; i System.out.print(counts [i]);
}
}

请注意,如果用户输入的值超过200,您还会得到一个 ArrayIndexOutOfBoundsException ,因为 MAX_NUM_OF_VALUES 是200,但是您无法实际执行。 / p>

I am trying to count occurrences of integers in an array. I would like to be able to modify the following loop that counts characters...

for (int i = 0; i < chars.length; i++)
     counts[chars[i] - 'a']++;

I tried modifying it like this but getting an ArrayIndexOutOfBoundsException error...

for (int i = 0; i < ints.length; i++)
     counts[ints[i] - '0']++;

Any ideas?

I have searched through many questions with similar titles but not in the fashion I need.

UPDATED

Here is my updated code. The user enters integers numbered 1 through 25. The program is supposed to count the occurrences of those integers using another array. I updated the code using...

counts[vals[i] - 1]++;

Which allows the program to compile. However, all the values of count are zeros.

UPDATED CODE...

import java.util.Scanner;


class CountIntOccurs
{
   private static final int MAX_NUM_OF_VALUES = 200;

   public static void main ( String [] args )
   {
      Scanner in = new Scanner( System.in );
      int[] vals = new int[MAX_NUM_OF_VALUES];
      int numOfVals = 0;

      // Read and store values
      System.out.println( "Enter integers <= 25, one per line, hit control-Z when done: " );
      while ( in.hasNextLine() )
      {
         vals[numOfVals] = Integer.parseInt( in.nextLine() );
         ++numOfVals;
      }

      // Close application if no values entered
      if ( numOfVals == 0 )
         System.exit( 0 );


      //  declare counts array   
      int[] counts = new int[25];


      System.out.println( counts.length );

      //??????? For each integer in the array, count it
      for (int i = 0; i < counts.length; i++){
         counts[vals[i] - 1]++; 
      }



      //display values enetered
      for ( int i = 0; i < numOfVals; ++i )
      {
            System.out.println( vals[i] );
      }


      //?????? display counted integers????
      for ( int i = 0; i < counts.length; ++i )
      {
            System.out.println( counts[i] );
      }


   }
}

Right now , what i need is to display the occurrences of the integers that are input. If I type in 1 1 2 2 2 the results should be 2 3 0 0 0 ... 0 .

解决方案

for (int i = 0; i < counts.length; i++) {
    counts[vals[i]-1]++; 
}

This is the problem, because you have counts.length instead of numOfVals. The loop is supposed to increase the count of some number by 1 for each entered value, so you have to keep looping until you have counted every entered value.

If you use counts.length, you will get ArrayIndexOutOfBoundsException if numOfVals is smaller than 25 (because counts.length is initialized to 25). If numOfVals is bigger than 25, you will only count the first 25 inputs.

The solution is therefore:

for (int i = 0; i < numOfVals; i++) {
    counts[vals[i]-1]++; 
}

Now, you are printing every number on a new line by using println() instead of print(), and you aren't printing any text, so it is hard to interpret the output of the program. I made some superficial changes to the program to make it easier to understand. Here's the complete program:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int[] vals = new int[MAX_NUM_OF_VALUES];
    int numOfVals = 0;

    System.out.println( "Enter integers <= 25, one per line, hit control-Z when done: " );
    while (in.hasNextLine()) {
        vals[numOfVals] = Integer.parseInt(in.nextLine());
        ++numOfVals;
    }
    if(numOfVals == 0)
        System.exit(0);

    int[] counts = new int[25];

    //changed counts.length to numOfVals
    for (int i = 0; i < numOfVals; i++) {
        counts[vals[i]-1]++; 
    }

    //display values entered
    System.out.print("Entered values: ");
    for (int i = 0; i < numOfVals; ++i) {
        System.out.print(vals[i]);
    }
    System.out.println(); //new line

    //display counted integers
    System.out.print("Counts of the values: ");
    for (int i = 0; i < counts.length; ++i) {
        System.out.print(counts[i]);
    }
}

Note that if the user enters more than 200 values, you will get an ArrayIndexOutOfBoundsException as well, because MAX_NUM_OF_VALUES is 200, but you have no way of actually enforcing that.

这篇关于计算Java数组中整数的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆