Galaxy
Galaxy

Reputation: 2481

How can I reduce this long list of if statements?

So here I have this long line of if statements, that are supposed to detect if the value of int[] anArray; is within a certain range. anArray = new int[15]; The values of int[] anArray;, starting from anArray[0] are: 49 50 51 59 0 5 9 10 15 19 50 55 89 99 100

This is the part of the code that determines if the given values are within a range:

int[] counterarray = new int[10];
    for (x = 14; x >= 0; x--)
    {
      System.out.println(anArray[x]);
      if (anArray[x] >= 0 && anArray[x] < 10)
      {
        counterarray[0] = counterarray[0] + 1;

      }
      if (anArray[x] >= 10 && anArray[x] < 20)
      {
        counterarray[1] = counterarray[1] + 1;

      }
      if (anArray[x] >= 20 && anArray[x] < 30)
      {
        counterarray[2] = counterarray[2] + 1;

      }
      if (anArray[x] >= 30 && anArray[x] < 40)
      {
        counterarray[3] = counterarray[3] + 1;

      }
      if (anArray[x] >= 40 && anArray[x] < 50)
      {
        counterarray[4] = counterarray[4] + 1;

      }
      if (anArray[x] >= 50 && anArray[x] < 60)
      {
        counterarray[5] = counterarray[5] + 1;

      }
      if (anArray[x] >= 60 && anArray[x] < 70)
      {
        counterarray[6] = counterarray[6] + 1;

      }
      if (anArray[x] >= 70 && anArray[x] < 80)
      {
        counterarray[7] = counterarray[7] + 1;

      }
      if (anArray[x] >= 80 && anArray[x] < 90)
      {
        counterarray[8] = counterarray[8] + 1;

      }
      if (anArray[x] >= 90 && anArray[x] < 101)
      {
        counterarray[9] = counterarray[9] + 1;

      }
    }
    System.out.println("counterarray[0] is " +counterarray[0]);
    System.out.println("counterarray[1] is " +counterarray[1]);
    System.out.println("counterarray[2] is " +counterarray[2]);
    System.out.println("counterarray[3] is " +counterarray[3]);
    System.out.println("counterarray[4] is " +counterarray[4]);
    System.out.println("counterarray[5] is " +counterarray[5]);
    System.out.println("counterarray[6] is " +counterarray[6]);
    System.out.println("counterarray[7] is " +counterarray[7]);
    System.out.println("counterarray[8] is " +counterarray[8]);
    System.out.println("counterarray[9] is " +counterarray[9]);

Yeah so that's the code, but that long list of if statements seems a bit redundant. The for loop flips through each of the array values and determines what range they belong in. Then the int[] counterarray adds up the amount of values together. So how do I make that long list of if statements more aesthetically pleasing?

Upvotes: 7

Views: 267

Answers (1)

Buddy
Buddy

Reputation: 11028

int[] counterarray = new int[10];
for (x = 14; x >= 0; x--)
{
  if (anArray[x] >= 0 && anArray[x] < 101) {
    int idx = Math.min(anArray[x] / 10, 9);
    ++counterarray[idx];
  }
}

If all the ranges were multiples of 10 (e.g. 0-9, 10-19, 20-29, etc), then we could just do a simple divide by 10 to get the index into counterarray. The Math.min part is to handle the odd last case which has the (original) range of 90-100; in the case of 100, idx would be equal to 10, but the Math.min clamps it so that it won't be an out-of-bounds index into the array.

The if check is to make sure we only look at values within the range that we expect (in this case 0-100). Otherwise we might erroneously increment the last bucket for large values (e.g. 200).

Upvotes: 18

Related Questions