-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataStructure.java
More file actions
94 lines (79 loc) · 2.24 KB
/
DataStructure.java
File metadata and controls
94 lines (79 loc) · 2.24 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Created by rism on 8/6/14.
*/
import java.util.*;
public class DataStructure
{
// Create an array
private final static int SIZE = 15;
private int[] arrayOfInts = new int[SIZE];
public DataStructure()
{
// fill the array with ascending integer values
for (int i = 0; i < SIZE; i++)
{
arrayOfInts[i] = i;
}
}
public void printEven()
{
final int count = 1;
// Print out values of even indices of the array
DataStructureIterator iterator = new DataStructureIterator()
{
@Override
public boolean hasNext()
{
int i = count;
return false;
}
@Override
public Integer next()
{
return null;
}
@Override
public void remove()
{
}
};
while (iterator.hasNext())
{
System.out.print(iterator.next() + " ");
}
System.out.println();
}
interface DataStructureIterator extends Iterator<Integer>
{}
// Inner class implements the DataStructureIterator interface,
// which extends the Iterator<Integer> interface
private class EvenIterator implements DataStructureIterator
{
// Start stepping through the array from the beginning
private int nextIndex = 0;
public boolean hasNext()
{
// Check if the current element is the last in the array
return (nextIndex <= SIZE - 1);
}
@Override
public void remove()
{
}
public Integer next()
{
// Record a value of an even index of the array
Integer retValue = Integer.valueOf(arrayOfInts[nextIndex]);
// Get the next even element
nextIndex += 2;
return retValue;
}
}
public static void main(String s[])
{
// Fill the array with integer values and print out only
// values of even indices
DataStructure ds = new DataStructure();
ds.printEven();
}
}