-
Notifications
You must be signed in to change notification settings - Fork 43
/
DataStructure.java
55 lines (41 loc) · 1.51 KB
/
DataStructure.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
package nestedClassExample.anonymousInnerClass;
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) {
// An anonymous inner class that implements the Iterator pattern.
return new Iterator() {
// Start stepping through the array from the beginning
private int next = 0;
@Override
public boolean hasNext() {
// Check if a current element is the last in the array
return (next <= arrayOfInts.length - 1);
}
@Override
public int getNext() {
// Get the value to be returned
int retValue = arrayOfInts[next];
// Increment the counter in preparation for the next call
next += increment;
return retValue;
}
};
}
}