简体   繁体   中英

Finding how many times an item Appears in an Array

Given an array of integers ranging from 1 to 60, i'm attempting to find how many times the numbers 1-44 appear in the array. Here is my method

public static void mostPopular(int[] list, int count)
    {
        int[] numbers;
        numbers = new int[44];
        for (int i = 0; i<count;i++)
        {
            if (list[i]<45 )
            {
                numbers[i-1]=numbers[i-1]+1; //error here
            }
        }
        for (int k=0; k<44;k++)
        {
            System.out.println("Number " + k + " occurs " + numbers[k-1]+ "times");
        }
    }

I'm trying to iterate through the array, list, that contains over 5000 numbers that are between 1-60, then test if that number is less than 45 making it a number of interest to me, then if the integer is 7 for example it would increment numbers[6] By 1. list is the array of numbers and count is how many total numbers there are in the array. I keep getting an ArrayIndexOutOfBoundsException. How do I go about fixing this?

Replace this line numbers[i-1]=numbers[i-1]+1;

with numbers[list[i] - 1] = numbers[list[i] - 1] + 1;

Now it will update the count of correct element.

You need to increment numbers[list[i]] because that's your value which is smaller than 45. i goes up to 5000 and your array numbers is too small.

You should really start using a debugger. All the modern IDE have support for it (Eclipse, IntelliJ, Netbeans, etc.). With the debugger you would have realized the mistake very quickly.

If your initial value is less than 45, it will add 1 to numbers[i-1]. However, since you start with i=0, it will try to add 1 to the value located at numbers[-1], which doesn't exist by law of arrays. Change i to start at 1 and you should be okay.

When i is 0 , i-1 is -1 -- an invalid index. I think that you want the value from list to be index into numbers . Additionally, valid indices run from 0 through 43 for an array of length 44 . Try an array of length 45 , so you have valid indices 0 through 44 .

numbers = new int[45];

and

if (list[i] < 45)
{
    // Use the value of `list` as an index into `numbers`.
    numbers[list[i]] = numbers[list[i]] + 1;
}

Very close, but a few indexing errors, remember 0-1 = -1, which isn't an available index. Also, this isn't c, so you can call list.length to get the size of the list. Try this (you can ignore the stuff outside of the mostPopular method):

class Tester{

   public static void main(String args[]){
       int[] list = new int[1000];
       Random random = new Random();
       for(int i=0; i<list.length; i++){
           list[i] = random.nextInt(60) + 1;
       }
       mostPopular(list);
   }

   public static void mostPopular(int[] list)
   {
       int[] numbers = new int[44];
       for (int i = 0; i< list.length ;i++)
       {
           int currentInt = list[i];

           if(currentInt<45 )
           {
               numbers[currentInt - 1] = (numbers[currentInt -1] + 1);
           }
       }
       for (int k=0; k<numbers.length; k++)
       {
           System.out.println("Number " + (k+1) + " occurs " + numbers[k]+ "times");
       }
   }

}

numbers[i-1]=numbers[i-1]+1; //error here 

change to

numbers[list[i]-1] += 1; 

as list[i]-1 because your number[0] store the frequency of 1 and so on.

we increase the corresponding array element with index equal to the list value minus 1

public static void mostPopular(int[] list, int count)
{
    int[] numbers = new int[44];
    for (int i = 0; i<count;i++)
    {
        //in case your list value has value less than 1
        if ( (list[i]<45) && (list[i]>0) )
        {
            //resolve error
            numbers[list[i]-1] += 1; 
        }
    }
    //k should start from 1 but not 0 because we don't have index of -1
    //k < 44 change to k <= 44 because now our index is 0 to 43 with [k-1]
    for (int k=1; k <= 44;k++)
    {
        System.out.println("Number " + k + " occurs " + numbers[k-1]+ "times");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM