-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathDataStructure.java
More file actions
70 lines (52 loc) · 1.88 KB
/
DataStructure.java
File metadata and controls
70 lines (52 loc) · 1.88 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
62
63
64
65
66
67
68
69
70
package nestedClassExample.staticInnerClass;
import nestedClassExample.Iterator;
public class DataStructure {
private int[] arrayOfInts;
public static void main(String s[]) {
// Create an instance and fill it with with integer values
DataStructure ds = new DataStructure(10);
// Iterate through the data structure and print it's values
Iterator iterator = ds.iterator(2);
while (iterator.hasNext()) {
System.out.println(iterator.getNext() + " ");
}
}
public DataStructure(int size) {
arrayOfInts = new int[size];
// Fill the array with ascending integer values
for (int i = 0; i < size; i++) {
arrayOfInts[i] = i;
}
}
public Iterator iterator(int increment) {
return new DataStructureIterator(arrayOfInts, increment);
}
/**
* A static inner class that implements the Iterator pattern. Notice that the static
* inner class has no access to the outer object's fields, so we must pass all the
* data it needs into its constructor.
*/
private static class DataStructureIterator implements Iterator {
private int[] array;
int increment;
// Start stepping through the array from the beginning
private int next = 0;
DataStructureIterator(int[] array, int increment) {
this.array = array;
this.increment = increment;
}
@Override
public boolean hasNext() {
// Check if a current element is the last in the array
return (next <= array.length - 1);
}
@Override
public int getNext() {
// Get the value to be returned
int retValue = array[next];
// Increment the counter in preparation for the next call
next += increment;
return retValue;
}
}
}