-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexedArrayList.java
More file actions
147 lines (131 loc) · 3.3 KB
/
IndexedArrayList.java
File metadata and controls
147 lines (131 loc) · 3.3 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import java.util.Iterator;
import java.util.ListIterator;
/**
* Implementation of a list that keeps track of a position within
* itself underlying ArrayList and keeping track of a position
* within that list.
*
* @author Chris Blades
* @version 13/3/2010
*/
public class IndexedArrayList<E> {
/** Underlying List */
private List<E> list;
/** used to keep track of our position within the list */
private int index;
/**
* Create a new empty IndexedArrayList with position = 0.
*
* /
public IndexedArrayList() {
index = 0;
list = new ArrayList<E>(0);
}
/**
* Creates a new IndexedArrayList with the given List and sets
* position = 0.
*
* @param toAdd the list to add to this list
*/
public IndexedArrayList(E[] toAdd) {
list = new ArrayList<E>(toAdd.length);
for (int i = 0; i < toAdd.length; i++) {
list.add(toAdd[i]);
}
index = 0;
}
/**
* Returns the current position within the list
*
* @return the position within the list
*/
public int getPosition() {
return this.index;
}
/**
* Sets the position within thie list.
*
* @param index new position within the list
*/
public void setPosition(int index) {
this.index = index;
}
/**
* Gets the element at the current position in the list and increments
* the position or wraps back to the beginning of the list if position
* is greater than the length of the list.
*
* @return the next element in the list
*/
public E getNext() {
E next = list.get(index);
index = (index + 1) % list.size();
return next;
}
/**
* Reverses the position within the list by 1.
*/
public void rewind() {
index--;
}
//---------List----------
/**
* Adds an element to the list at the specified index
*
* @param index where to add the element
* @param element the element to add to the list
*/
public void add(int index, E element) {
list.add(index, element);
}
/**
* Adds a new element to the end of the list.
*
* @param element the object to add to the list
*/
public boolean add(E o) {
return list.add(o);
}
/**
* copies the elements of the given collection onto the end of this list.
*
* @param c collection of elements to add
*/
public boolean addAll(Collection<E> c) {
return list.addAll(c);
}
/**
* Deletes all elements from the list and resets the position to 0
*/
public void clear() {
list.clear();
this.index = 0;
}
/**
* Returns wether or not this list is empty.
*
* @return true if this list is empty
*/
public boolean isEmpty() {
return list.isEmpty();
}
/**
* Removes the element at the specified position in the list
*
* @param index the index of the element to remove
*/
public Object remove(int index) {
return list.remove(index);
}
/**
* Returns the number of elements in the list
*
* @param the number of elements in the list
*/
public int size() {
return list.size();
}
}