'How to get array elements in frequency in between two array?

int []data = new int [SamData.length];
int count = 0;
for(int i=0;i<sampleInt;i++) {
    for(int f=0;f<UpLimit[0]; f++, UpLimit[0]++){
        if(20 <= UpLimit[i]){
            count++;
        }
    }
System.out.println(LowLimit[i] + "\t\t" + UpLimit[i] + "\t\t\t" + count);
}
}
  1. what I need is if the user inputted values, for example...

    value1: 8 value2: 16 value3: 3 value4: 13 value5: 24

    it should check if the values are in between the upper and lower numbers. for example

    0-9 = 2 //(3,8) 10-20 = 2 //(13,16) 20-30 = 1 //(24) 30-40 = 0 and so on...

    but in the code it just show... 0-9 = 1 10-20 = 2
    20-30 = 3
    30-40 = 4 and so on.



Solution 1:[1]

Here's how I would do it. First, create a class to handle your bounds objects:

public static class Bounds {
    private int lowerBounds;
    private int upperBounds;
    private int count = 0;
    
    public Bounds(int low, int high) {
        lowerBounds = low;
        upperBounds = high;
    }
    
    public int getLowerBounds() {
        return lowerBounds;
    }

    public int getUpperBounds() {
        return upperBounds;
    }

    public void checkValue(int value) {
        if (lowerBounds <= value && value < upperBounds) {
            count++;
        }
    }
    
    public String toString() {
        return lowerBounds + "\t\t" + upperBounds + "\t\t\t" + count;
    }
}

Notice, this also handles string output.

Then initialize your bounds. I'm not following exactly your bounds, but you can fix that to do what you need:

List<Bounds> boundsList= new ArrayList<>();
for (int i = 0; i < 100; i+=10) {
    boundsList.add(new Bounds(i, i+9));
}

Then you can do your calculations

 for (int value : values) {
    for (Bounds bounds : boundsList) {
        bounds.checkValue(value);
    }
}

Then you can output:

for (Bounds bounds : boundsList) {
    System.out.println(bounds);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryan