KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > ArrayIterator


1 /*
2  * Converts an array to an iterator.
3  * Copyright (C) 2004 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18
19 package com.Ostermiller.util;
20
21 import java.util.*;
22
23 /**
24  * Converts an array to an iterator.
25  * <p>
26  * More information about this class is available from <a target="_top" HREF=
27  * "http://ostermiller.org/utils/Iterator_Enumeration.html">ostermiller.org</a>.
28  *
29  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
30  * @since ostermillerutils 1.03.00
31  */

32 public class ArrayIterator <ElementType> implements Iterator {
33
34     /**
35      * Array being converted to iterator.
36      */

37     private ElementType[] array;
38
39     /**
40      * Current index into the array.
41      */

42     private int index = 0;
43
44     /**
45      * Whether the last element has been removed.
46      */

47     private boolean lastRemoved = false;
48
49     /**
50      * Create an Iterator from an Array.
51      *
52      * @param array of objects on which to enumerate.
53      *
54      * @since ostermillerutils 1.03.00
55      */

56     public ArrayIterator(ElementType[] array){
57         this.array = array;
58     }
59
60     /**
61      * Tests if this Iterator contains more elements.
62      *
63      * @return true if and only if this Iterator object contains at least
64      * one more element to provide; false otherwise.
65      *
66      * @since ostermillerutils 1.03.00
67      */

68     public boolean hasNext(){
69         return (index < array.length);
70     }
71
72     /**
73      * Returns the next element of this Iterator if this Iterator
74      * object has at least one more element to provide.
75      *
76      * @return the next element of this Iterator.
77      * @throws NoSuchElementException if no more elements exist.
78      *
79      * @since ostermillerutils 1.03.00
80      */

81     public ElementType next() throws NoSuchElementException {
82         if (index >= array.length) throw new NoSuchElementException("Array index: " + index);
83         ElementType object = array[index];
84         index++;
85         lastRemoved = false;
86         return object;
87     }
88
89     /**
90      * Removes the last object from the array by setting the slot in
91      * the array to null.
92      * This method can be called only once per call to next.
93      *
94      * @throws IllegalStateException if the next method has not yet been called, or the remove method has already been called after the last call to the next method.
95      *
96      * @since ostermillerutils 1.03.00
97      */

98     public void remove(){
99         if (index == 0) throw new IllegalStateException JavaDoc();
100         if (lastRemoved) throw new IllegalStateException JavaDoc();
101         array[index-1] = null;
102         lastRemoved = true;
103     }
104 }
Popular Tags