简体   繁体   中英

Finding the occurrence of values within an array

My question is how do I find the frequency of the numbers "8" and "88" in this array, using a method. It seems as what I put in the assessor method does not appear to work. For example, if "8" occurs three times in the array the output would be "3" and the same for "88". If I am wrong please point me to the right direction. Any help with my question is greatly appreciate.

import java.util.Random;

public class ArrayPractice {
    private int[] arr;
    private final int MAX_ARRAY_SIZE = 300;
    private final int MAX_VALUE = 100;

    public ArrayPractice() {
        // initialize array
        arr = new int[MAX_ARRAY_SIZE];

        // randomly fill array with numbers
        Random rand = new Random(1234567890);
        for (int i = 0; i < MAX_ARRAY_SIZE; ++i) {
            arr[i] = rand.nextInt(MAX_VALUE) + 1;
        }
    }


    public void printArray() {
        for (int i = 0; i < MAX_ARRAY_SIZE; ++i) 
            System.out.println(arr[i]);
    }


    public int countFrequency(int value) {
    for (int i: MAX_VALUE) {
        if (i == 8) 
            i++;
    }



    public static void main(String[] args) {
        ArrayPractice ap = new ArrayPractice();

        System.out.println("The contents of my array are: ");
        ap.printArray();
        System.out.println("");

        System.out.println("The frequency of 8 is: " + ap.countFrequency(8));
        System.out.println("The frequency of 88 is: " + ap.countFrequency(88));

        }
    }
}

You need to iterate over arr and increment a variable when an element matches value .

public int countFrequency(int value) {
    int count = 0;
    for (int num : arr) {
        if (num == value) {
            count++;
        }
    }
    return count;
}

Solution:

public int countFrequency(int value) {
    int counter = 0; // here you will store counter of occurences of value passed as argument
    for (int i : arr) { // for each int from arr (here was one of your errors)
        if (i == value) // check if currently iterated int is equal to value passed as argument
            counter++; // if it is equal, increment the counter value
    }
    return counter; // return the result value stored in counter
}

Explanation:

Main problem in your code was countFrequency() method, there are few bugs that you need to change if you want to make it work correctly:

  1. You passed value as argument and you didn't even use it in the body of method.

  2. for (int i : MAX_VALUE ) - you meant to iterate over elements of arr array, (You can read it then as: For each int from arr array do the following: {...}.

  3. if (i == 8) i++ - here you said something like this: Check if the current element from array (assuming that you meant MAX_VALUE is an array) is equal to 8, and if it is - increment this value by 1 (so if it were 8, now it's 9). Your intention here was to increment counter that counts occurences of 8.

You might want to consider making these improvements to countFrequency() method, to make it work properly.

You have a hard-coded seed, so your random values won't be random on different runs. You are also hard-coding your frequency count against 8 (instead of value ). But honestly, I suggest you revisit this code with lambdas (as of Java 8), they make it possible to write the array generation, the print and the count routines in much less code. Like,

public class ArrayPractice {
    private int[] arr;
    private final int MAX_ARRAY_SIZE = 300;
    private final int MAX_VALUE = 100;

    public ArrayPractice() {
        // randomly fill array with numbers
        Random rand = new Random();
        arr = IntStream.generate(() -> rand.nextInt(MAX_VALUE) + 1)
                .limit(MAX_ARRAY_SIZE).toArray();
    }

    public void printArray() {
        IntStream.of(arr).forEachOrdered(System.out::println);
    }

    public int countFrequency(int value) {
        return (int) IntStream.of(arr).filter(i -> i == value).count();
    }
}

You need to iterate over the array and increment a counter variable when an element matches i

what you are doing is increment i instead of a counter:

if (i == 8)
  i++;
}     // if i is 8 then i becomes 9 

A working example:

public int countFrequency(int i) {
int count = 0;
for (int num : arr) {
    if (num == i) {
        count++;
    }
}
return count;
}

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