Skipping numbers in a for loop in Java. How can I make this more clean?
Solution 1
You can also simply add if (i % 11 == 2) continue;
at the start of the loop:
for (int i = 305; i < 397; i++) {
if (i % 11 == 2) continue;
//do stuff
}
This is because you want to skip a number every 11 steps, starting at 310.
So, you want to skip numbers for which i - 310
is a multiple of 11, so for which i - 310 + 11*X
is a multiple of 11 whatever X is. So, I've chosen 28 for X, because i - 310 + 11 * 28
equals i - 2
. This is easier to read. Finally, the test is simply (i - 2) % 11 == 0
, and is equivalent to i % 11 == 2
. This method of calculus is named basic modular arithmetic.
Solution 2
If you're on java 8, You can use the new streams api with IntStream. You should also use a Set to check for membership quickly. Sets.newHashSet
is a helper from guava
, but you can also just create a HashSet by hand.
Set<int> intsToSkip = Sets.newHashSet( 310, 321, 332, 343, 354, 365, 376, 387 );
IntStream.range(305, 397)
.filter(i => ! intsToSkip.contains(i))
.forEach(i => {
// do stuff
})
Solution 3
The Java8 solution could look like this:
List<Intger> valuesToProcess = IntStream.range(305,397).collect(Collectors.toList());
valuesToProcess.removeAll(Arrays.asList(310, 321, 332, 343, 354, 365, 376, 387 ));
for(Integer value : valuesToProcess) {
// do stuff here
}
Solution 4
The numbers to be skipped are those with i % 11 == 2
.
Using this, the Java 8 way is:
IntStream.range(305, 397)
.filter(i -> i % 11 != 2)
.forEach(i -> {
// do stuff
});
And for older Java versions do:
for (int i = 305; i < 397; i++) {
if (i % 11 != 2) {
// do stuff
}
}
Solution 5
There seems to be a pattern of the numbers you need to ignore.
When the 2nd digit is exactly one higher than the 3rd digit, you ignore the number.
for (int i = 305; i < 397; i++) {
String s = String.valueOf(i);
if (s.charAt(1) == s.charAt(2) + 1) {
continue; // ignore
}
//do stuff
}
David
Updated on June 21, 2022Comments
-
David almost 2 years
I need to create a for loop starting at 305 and ending at 397.
for (int i = 305; i < 397; i++) { //do stuff }
The problem is, I need to skip 310, 321, 332, 343, 354, 365, 376, and 387.
I know I can do this by placing the following in the loop:
int[] skip = { 310, 321, 332, 343, 354, 365, 376, 387 }; for (int i2 : skip) { if (i != i2) { //do stuff } }
But I feel like there is a much more efficient, clean way to achieve this.