An Iterator wrapper for an Object[], allow us to deal with all array like structures in a consistent manner. : Iterator « Collections Data Structure « Java






An Iterator wrapper for an Object[], allow us to deal with all array like structures in a consistent manner.

    


/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.lang.reflect.Array;


/**
 *  <p>
 *  An Iterator wrapper for an Object[]. This will
 *  allow us to deal with all array like structures
 *  in a consistent manner.
 *  </p>
 *  <p>
 *  WARNING : this class's operations are NOT synchronized.
 *  It is meant to be used in a single thread, newly created
 *  for each use in the #foreach() directive.
 *  If this is used or shared, synchronize in the
 *  next() method.
 *  </p>
 *
 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
 * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
 * @version $Id: ArrayIterator.java 463298 2006-10-12 16:10:32Z henning $
 */
public class ArrayIterator implements Iterator
{
    /**
     * The objects to iterate.
     */
    private Object array;

    /**
     * The current position and size in the array.
     */
    private int pos;
    private int size;

    /**
     * Creates a new iterator instance for the specified array.
     *
     * @param array The array for which an iterator is desired.
     */
    public ArrayIterator(Object array)
    {
        /*
         * if this isn't an array, then throw.  Note that this is
         * for internal use - so this should never happen - if it does
         *  we screwed up.
         */

        if ( !array.getClass().isArray() )
        {
            throw new IllegalArgumentException(
                "Programmer error : internal ArrayIterator invoked w/o array");
        }

        this.array = array;
        pos = 0;
        size = Array.getLength( this.array );
    }

    /**
     * Move to next element in the array.
     *
     * @return The next object in the array.
     */
    public Object next()
    {
        if (pos < size )
            return Array.get( array, pos++);

        /*
         *  we screwed up...
         */

        throw new NoSuchElementException("No more elements: " + pos +
                                         " / " + size);
    }

    /**
     * Check to see if there is another element in the array.
     *
     * @return Whether there is another element.
     */
    public boolean hasNext()
    {
        return (pos < size );
    }

    /**
     * No op--merely added to satify the <code>Iterator</code> interface.
     */
    public void remove()
    {
        throw new UnsupportedOperationException();
    }
}

   
    
    
    
  








Related examples in the same category

1.Listing the Elements of a Collection
2.De-mystify the Iterator interface, showing how to write a simple Iterator for an Array of Objects
3.Iterate over Set
4.Demonstrate iterators.
5.Use the for-each for loop to cycle through a collection.
6.List IteratorList Iterator
7.Iterate a Collection and remove an item (Exception, wrong version)
8.Use an Iterator and remove the item with Iterator.remove()
9.An Iterator wrapper for an Enumeration.
10.EmptyIterator is an iterator which is empty.
11.Implements an java.util.Iterator over any array
12.Treat an Iterator as an Iterable
13.Iterator class for sparse values in an array.
14.Iterator class for values contained in an array range.
15.Array Iterator
16.Cyclic Iteration
17.Create singleton Iterator
18.Empty Iterator
19.An Iterator that wraps a number of Iterators
20.An Iterator to iterate over the elements of an array
21.Sorted Iterator
22.Iterator Union of Iterators
23.Iterator Utils
24.Linked Iterator
25.Prefetch Iterator
26.Protects an given iterator by preventing calls to remove().
27.An Iterator that returns the elements of a specified array, or other iterators etc.
28.An array iterator
29.Adapt iterator to iterable
30.Static utility methods, classes, and abstract classes for iteration.
31.Iterator Collection
32.Convert Iterable to List
33.A singleton null object Iterator implementation.