-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunIterator.java
131 lines (121 loc) · 4.99 KB
/
RunIterator.java
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* RunIterator.java */
/**
* The RunIterator class iterates over a RunLengthEncoding and allows other
* classes to inspect the runs in a run-length encoding, one run at a time.
* A newly constructed RunIterator "points" to the first run in the encoding
* used to construct it. Each time next() is invoked, it returns a run
* (represented as an array of four ints); a sequence of calls to next()
* returns run in consecutive order until every run has been returned.
*
* Client classes should never call the RunIterator constructor directly;
* instead they should invoke the iterator() method on a RunLengthEncoding
* object, which will construct a properly initialized RunIterator for the
* client.
*
* Calls to hasNext() determine whether another run is available, or whether
* the iterator has reached the end of the run-length encoding. When
* a RunIterator reaches the end of an encoding, it is no longer useful, and
* the next() method may throw an exception; thus it is recommended to check
* hasNext() before each call to next(). To iterate through the encoding
* again, construct a new RunIterator by invoking iterator() on the
* RunLengthEncoding and throw the old RunIterator away.
*
* A RunIterator is not guaranteed to work if the underlying RunLengthEncoding
* is modified after the RunIterator is constructed. (Especially if it is
* modified by setPixel().)
*/
import java.util.Iterator;
import java.util.NoSuchElementException;
@SuppressWarnings("rawtypes")
public class RunIterator implements Iterator {
/**
* Define any variables associated with a RunIterator object here.
* These variables MUST be private.
*/
private SListNode currentNode;
/**
* RunIterator() constructs a new iterator starting with a specified run.
*
* @param node the run where this iterator starts.
*/
// Unlike all the other methods we have asked you to write, the RunIterator()
// constructor does not have a predefined signature, because no outside
// class should ever call this constructor except the iterator() method in
// the RunLengthEncoding class. The correct way for outside classes to
// get access to a RunIterator is to call the iterator() method on a
// RunLengthEncoding object. You are welcome to add any parameters to the
// constructor that you want so that your RunLengthEncoding.iterator()
// implementation can construct a RunIterator that points to the first run of
// the encoding.
RunIterator(SListNode e) {
// Your solution here. You may add parameters to the method signature.
currentNode = e;
}
public Object getItem(){
return currentNode.item;
}
public String toString(){
String s ="";
for(int i=0;i<((int[])this.getItem()).length;i++){
s = s.concat(((int[])this.getItem())[i]+" ");
}
return s;
}
/**
* hasNext() returns true if this iterator has more runs. If it returns
* false, then the next call to next() may throw an exception.
*
* @return true if the iterator has more elements.
*/
public boolean hasNext() {
// Replace the following line with your solution.
if(currentNode.next == null){
return false;
}
return true;
}
/**
* next() returns an array of 4 ints that specifies the current run in the
* sequence. It also advances the iterator to the next run, so that the
* next call to next() will return the following run.
*
* If "this" RunIterator has returned every run, it cannot be expected to
* behave well. (Technically, it is supposed to throw a
* NoSuchElementException, but we haven't learned about exceptions yet.)
*
* @return an array of 4 ints that specify the current run in the sequence.
* The pixel count is in index [0]; the red value is in index [1]; the green
* value is in index [2]; and the blue value is in index [3].
* @throws NoSuchElementException if the iteration has no more elements.
* (We strongly recommend calling hasNext() to check whether there are any
* more runs before calling next().)
*
* The returned four-int array is constructed in next(), and can be
* discarded by the calling method after use. The array should not be part
* of your RunLengthEncoding data structure! It must be freshly constructed
* for the sole purpose of returning four ints.
*/
public int[] next() {
// Construct a new array of 4 ints, fill in its values, and return it.
// Don't forget to advance the RunIterator's pointer so that the next
// call to next() will return the subsequent run.
// Replace the following line with your solution.
int[] temp = new int[4];
for(int i=0;i<4;i++){
temp[i] = ((int[])currentNode.item)[i];
}
if(this.hasNext()){
currentNode = currentNode.next;
}
return new int[4];
}
/**
* remove() would remove from the underlying run-length encoding the run
* identified by this iterator, but we are NOT implementing it.
*
* DO NOT CHANGE THIS METHOD.
*/
public void remove() {
throw new UnsupportedOperationException();
}
}