-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayIterator.java
More file actions
62 lines (54 loc) · 1.58 KB
/
ArrayIterator.java
File metadata and controls
62 lines (54 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.*;
public class ArrayIterator<T> implements Iterator
{
private int count; // the number of elements in the collection
private int current; // the current position in the iteration
private T[] items;
/**
* Sets up this iterator using the specified items.
*
* @param collection the collection for which the iterator will be created
* @param size the size of the collection
*/
public ArrayIterator (T[] collection, int size)
{
items = collection;
count = size;
current = 0;
}
/**
* Returns true if this iterator has at least one more element
* to deliver in the iteraion.
*
* @return true if this iterator has at least one more element to deliver
*/
public boolean hasNext()
{
return (current < count);
}
/**
* Returns the next element in the iteration. If there are no
* more elements in this itertion, a NoSuchElementException is
* thrown.
*
* @return the next element in the iteration
* @throws NoSuchElementException if a no such element exception occurs
*/
public T next()
{
if (! hasNext())
throw new NoSuchElementException();
current++;
return items[current - 1];
}
/**
* The remove operation is not supported in this collection.
*
* @throws UnsupportedOperationException if an unsupported operation
* exception occurs
*/
public void remove() throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
}