How can I represent integer spans in Java?

We all know about intervals in mathematics (e.g. -4 <x <3).

How can I represent these mathematical intervals in Java, for example numbers from -4 to 3 (-4, -3, ..., 2, 3, etc.)?

+5
source share
3 answers

Check apache commons-lang IntRange. So, if you want to check if a number is in a given interval (range), follow these steps:

IntRange range = new IntRange(-4, 3);
if (range.contains(x)) {
   ....
}
+12
source

You just need to separate -4 < x < 3before -4 < xand x < 3for example:

if (-4 < x && x < 3)
{
. . .
}
+3
source
+2
source

All Articles