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?
You could maintain a mapping of strings to enums elsewhere in the program, pull out the enum associated with the returned string from the map (with a default NO_MATCH in case the string isn't in the map) and write a switch statement on the enums.
Use If/Else Statements in One Line To simplify the code and reduce the number of lines in the conditional statements, you can use an inline if conditional statement. Consider the following concise code that performs the same with one line for each if/else conditional statement.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With