KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > utils > ArrayIterator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.utils;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.NoSuchElementException JavaDoc;
15
16 /**
17  * An object that iterates over the elements of an array
18  */

19 public class ArrayIterator implements Iterator JavaDoc {
20     Object JavaDoc[] elements;
21     int index;
22     int lastElement;
23
24     /**
25      * Returns new array enumeration over the given object array
26      */

27     public ArrayIterator(Object JavaDoc[] elements) {
28         this(elements, 0, elements.length - 1);
29     }
30
31     /**
32      * Returns new array enumeration over the given object array
33      */

34     public ArrayIterator(Object JavaDoc[] elements, int firstElement, int lastElement) {
35         super();
36         this.elements = elements;
37         index = firstElement;
38         this.lastElement = lastElement;
39     }
40
41     /**
42      * Returns true if this enumeration contains more elements.
43      */

44     public boolean hasNext() {
45         return elements != null && index <= lastElement;
46     }
47
48     /**
49      * Returns the next element of this enumeration.
50      * @exception NoSuchElementException if no more elements exist.
51      */

52     public Object JavaDoc next() throws NoSuchElementException JavaDoc {
53         if (!hasNext())
54             throw new NoSuchElementException JavaDoc();
55         return elements[index++];
56     }
57
58     public void remove() {
59         throw new UnsupportedOperationException JavaDoc();
60     }
61 }
62
Popular Tags