2 Replies Latest reply on Sep 11, 2007 3:51 PM by 796125

    Comparing integer ranges in java

    807605
      Dear all,

      I would like to ask if there is any direct method in java for comparing two integer ranges in java. E.g. i have a range 3 to 7 and a range 5 to 10, and i would to know if there is any overlapping.

      Thank you!
        • 1. Re: Comparing integer ranges in java
          800282
          Dear all,

          I would like to ask if there is any direct method in
          java for comparing two integer ranges in java. E.g. i
          have a range 3 to 7 and a range 5 to 10, and i would
          to know if there is any overlapping.

          Thank you!
          No there is no direct class/method to do that in Java. You will have to write your own class (which is pretty straight forward).
          • 2. Re: Comparing integer ranges in java
            796125
            Dear all,

            I would like to ask if there is any direct method in
            java for comparing two integer ranges in java. E.g. i
            have a range 3 to 7 and a range 5 to 10, and i would
            to know if there is any overlapping.

            Thank you!
            There is no direct method, but you can always write a quick method to do this:
            public class IntegerRange {
                public int lower;
                public int upper;
            
                public boolean intersects(IntegerRange other) {
                    if (containedIn(other)) return true;
                    if (contains(other)) return true;
                    if (contains(other.lower)) return true;
                    if (contains(other.upper)) return true;
                    return false;
                }
            
                public boolean contains(IntegerRange other) {
                    return contains(other.lower) && contains(other.upper);
                }
            
                public boolean containedIn(IntegerRange other) {
                    return other.contains(this);
                }
            
                public boolean contains(int i) {
                    return (i > lower) && (i < upper);
                }
            }
            - Adam