Class IntRange

java.lang.Object
it.unimi.di.prog2.s15.IntRange
All Implemented Interfaces:
Iterable<Integer>

public class IntRange extends Object implements Iterable<Integer>
A mutable class representing a range of integers, with configurable initial from, to, and step values.

The class is iterable; when the iterator is created, if the step is positive, the initial value must be less than the final value, and conversely if the step is negative; once created, the iterator is not affected by successive modification in the range.

  • Field Details

    • from

      private int from
      The first number of the iteration.
    • to

      private int to
      The last number of the iteration.
    • step

      private int step
      The iteration step.
  • Constructor Details

  • Method Details

    • from

      public void from(int from)
      Sets the initial value.
      Parameters:
      from - the initial value.
    • to

      public void to(int to)
      Sets the end value.
      Parameters:
      to - the end value.
    • step

      public void step(int step)
      Sets the step.
      Parameters:
      step - the step value.
      Throws:
      IllegalArgumentException - if the step is 0.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • iterator

      public Iterator<Integer> iterator()
      Returns an iterator for this range.

      If the range is modified after the iterator is returned, the modifications will not be reflected in the iterator.

      Specified by:
      iterator in interface Iterable<Integer>
      Returns:
      the iterator.
      Throws:
      IllegalArgumentException - if the initial value is less than the final, but the step is negative, or conversely if the initial value is greater than the final, but the step is positive.