Class ArrayIterator

  • All Implemented Interfaces:
    Iterator, ResettableIterator
    Direct Known Subclasses:
    ArrayListIterator

    public class ArrayIterator
    extends Object
    implements ResettableIterator
    Implements an Iterator over any array.

    The array can be either an array of object or of primitives. If you know that you have an object array, the ObjectArrayIterator class is a better choice, as it will perform better.

    The iterator implements a reset() method, allowing the reset of the iterator back to the start if required.

    Since:
    Commons Collections 1.0
    Version:
    $Revision: 647116 $ $Date: 2008-04-11 13:23:08 +0200 (Fri, 11 Apr 2008) $
    Author:
    James Strachan, Mauricio S. Moura, Michael A. Smith, Neil O'Toole, Stephen Colebourne
    • Field Detail

      • array

        protected Object array
        The array to iterate over
      • startIndex

        protected int startIndex
        The start index to loop from
      • endIndex

        protected int endIndex
        The end index to loop to
      • index

        protected int index
        The current iterator index
    • Constructor Detail

      • ArrayIterator

        public ArrayIterator()
        Constructor for use with setArray.

        Using this constructor, the iterator is equivalent to an empty iterator until setArray(Object) is called to establish the array to iterate over.

      • ArrayIterator

        public ArrayIterator​(Object array)
        Constructs an ArrayIterator that will iterate over the values in the specified array.
        Parameters:
        array - the array to iterate over.
        Throws:
        IllegalArgumentException - if array is not an array.
        NullPointerException - if array is null
      • ArrayIterator

        public ArrayIterator​(Object array,
                             int startIndex)
        Constructs an ArrayIterator that will iterate over the values in the specified array from a specific start index.
        Parameters:
        array - the array to iterate over.
        startIndex - the index to start iterating at.
        Throws:
        IllegalArgumentException - if array is not an array.
        NullPointerException - if array is null
        IndexOutOfBoundsException - if the index is invalid
      • ArrayIterator

        public ArrayIterator​(Object array,
                             int startIndex,
                             int endIndex)
        Construct an ArrayIterator that will iterate over a range of values in the specified array.
        Parameters:
        array - the array to iterate over.
        startIndex - the index to start iterating at.
        endIndex - the index to finish iterating at.
        Throws:
        IllegalArgumentException - if array is not an array.
        NullPointerException - if array is null
        IndexOutOfBoundsException - if either index is invalid
    • Method Detail

      • checkBound

        protected void checkBound​(int bound,
                                  String type)
        Checks whether the index is valid or not.
        Parameters:
        bound - the index to check
        type - the index type (for error messages)
        Throws:
        IndexOutOfBoundsException - if the index is invalid
      • hasNext

        public boolean hasNext()
        Returns true if there are more elements to return from the array.
        Specified by:
        hasNext in interface Iterator
        Returns:
        true if there is a next element to return
      • next

        public Object next()
        Returns the next element in the array.
        Specified by:
        next in interface Iterator
        Returns:
        the next element in the array
        Throws:
        NoSuchElementException - if all the elements in the array have already been returned
      • getArray

        public Object getArray()
        Gets the array that this iterator is iterating over.
        Returns:
        the array this iterator iterates over, or null if the no-arg constructor was used and setArray(Object) has never been called with a valid array.
      • setArray

        public void setArray​(Object array)
        Sets the array that the ArrayIterator should iterate over.

        If an array has previously been set (using the single-arg constructor or this method) then that array is discarded in favour of this one. Iteration is restarted at the start of the new array. Although this can be used to reset iteration, the reset() method is a more effective choice.

        Parameters:
        array - the array that the iterator should iterate over.
        Throws:
        IllegalArgumentException - if array is not an array.
        NullPointerException - if array is null
      • reset

        public void reset()
        Resets the iterator back to the start index.
        Specified by:
        reset in interface ResettableIterator