public class RangeResource
extends java.lang.Object
RangeResource
class represents a range of integer (or whole) numbers and allows
access to these numbers using the method sequence
. The integers can then be iterated
over using a for
loop
Example usage:
// this prints the square number sequence for the first 9 numbers: // 1, 4, 9, 16, 25, 36, 49, 64, 81 RangeResource rr = new RangeResource(1, 10); for (int value : rr.sequence()) { System.out.println(value * value); }
Another example usage:
// this prints the odd numbers in decreasing order within the given range: // 37, 35, 33, 31, 29, 27, 25, 23 RangeResource rr = new RangeResource(37, 21, -2); for (int value : rr.sequence()) { System.out.println(value); }
This software is licensed with an Apache 2 license, see http://www.apache.org/licenses/LICENSE-2.0 for details.
Constructor and Description |
---|
RangeResource(int end)
Create a
RangeResource object, starting at 0 and going up to but not including
end, [0 - end), that increments by 1. |
RangeResource(int start,
int end)
Create a
RangeResource object, starting at start and going up to but not
including end, [start - end), that increments by 1. |
RangeResource(int start,
int end,
int increment)
Create a
RangeResource object, starting at start and going up to but not
including end, [start - end), that increments by the amount passed as a parameter. |
RangeResource(RangeResource other)
Create an
RangeResource object that is a copy of another range. |
Modifier and Type | Method and Description |
---|---|
java.lang.Iterable<java.lang.Integer> |
sequence()
Allow access to the numbers in this range one at a time.
|
java.lang.String |
toString()
Return string representation of this range, with each value in the sequence separated by a
comma.
|
public RangeResource(int end)
RangeResource
object, starting at 0 and going up to but not including
end, [0 - end), that increments by 1.end
- when to stop the range, not included as one of the valuesexception
- if the end is negativepublic RangeResource(int start, int end)
RangeResource
object, starting at start and going up to but not
including end, [start - end), that increments by 1.start
- the first value in the range, included as one of the valuesend
- when to stop the range, not included as one of the valuesexception
- if the end is less than the startpublic RangeResource(int start, int end, int increment)
RangeResource
object, starting at start and going up to but not
including end, [start - end), that increments by the amount passed as a parameter.start
- the first value in the range, included as one of the valuesend
- when to stop the range, not included as one of the valuesincrement
- how much to add to get the next value in the range's sequenceexception
- if increment is negative when the end is greater than the startexception
- if increment is positive when the end is less than the startexception
- if increment is 0public RangeResource(RangeResource other)
RangeResource
object that is a copy of another range.other
- the original range being copiedpublic java.lang.String toString()
toString
in class java.lang.Object
String
containing all the values in the rangepublic java.lang.Iterable<java.lang.Integer> sequence()
Iterable
that will allow access to each number in this range