An enumeration that iterates over an array. : Enumerator « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Collections Data Structure » EnumeratorScreenshots 
An enumeration that iterates over an array.
An enumeration that iterates over an array.
   
/**
 
 * JFreeReport : a free Java reporting library
 
 *
 * Project Info:  http://reporting.pentaho.org/
 *
 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * ------------
 * ArrayEnumeration.java
 * ------------
 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
 */


import java.util.Enumeration;
import java.util.NoSuchElementException;

/**
 * An enumeration that iterates over an array.
 *
 @author Thomas Morgner

 */
public class ArrayEnumeration implements Enumeration
{
  /** The base datasource. */
  private Object[] objectarray = null;
  /** The counter holds the current position inside the array. */
  private int counter = 0;

  /**
   * Creates a new enumeration for the given array.
   *
   @param objectarray the array over which to iterate
   @throws NullPointerException if the array is null.
   */
  public ArrayEnumeration(final Object[] objectarray)
  {
    if (objectarray == null)
    {
      throw new NullPointerException("The array must not be null.");
    }

    this.objectarray = objectarray;
  }

  /**
   * Returns true if this enumeration has at least one more Element.
   *
   @return true, if there are more elements, false otherwise.
   */
  public boolean hasMoreElements()
  {
    return (counter < objectarray.length);
  }

  /**
   * Returns the next element in the Array.
   *
   @return the next element in the array.
   @throws  NoSuchElementException  if no more elements exist.
   */
  public Object nextElement()
  {
    if (counter >= objectarray.length)
    {
      throw new NoSuchElementException();
    }

    final Object retval = objectarray[counter];
    counter += 1;
    return retval;
  }
}

   
    
    
  
Related examples in the same category
1.Wrapping an Iterator around an Enumeration
2.A GOF Adapter to make instances of old Enumeration interface behave like new Iterator interface
3.A more robust enumeration systemA more robust enumeration system
4.ListOfFiles implements Enumeration
5.Treat an Enumeration as an Iterable
6.Support for breadth-first enumerating.
7.Empty Enumeration
8.Single Item Enumeration
9.Enumeration interface which enumerates the items of an arrayEnumeration interface which enumerates the items of an array
10.Filtering Enumeration
11.Concatenates the content of two enumerations into one.
12.Removes all nulls from the input enumeration.
13.Filters some elements out from the input enumeration.
14.For each element of the input enumeration asks the Processor to provide a replacement
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.