Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
249 lines (219 loc) · 5.17 KB

File metadata and controls

249 lines (219 loc) · 5.17 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* @author downey
* @param <T>
*
*/
public class MyArrayList<T> implements List<T> {
int size; // keeps track of the number of elements
private T[] array; // stores the elements
/**
*
*/
@SuppressWarnings("unchecked")
public MyArrayList() {
// You can't instantiate an array of T[], but you can instantiate an
// array of Object and then typecast it. Details at
// http://www.ibm.com/developerworks/java/library/j-jtp01255/index.html
array = (T[]) new Object[10];
size = 0;
}
/**
* @param args
*/
public static void main(String[] args) {
// run a few simple tests
MyArrayList<Integer> mal = new MyArrayList<Integer>();
mal.add(1);
mal.add(2);
mal.add(3);
System.out.println(Arrays.toString(mal.toArray()) + " size = " + mal.size);
mal.remove(new Integer(2));
System.out.println(Arrays.toString(mal.toArray()) + " size = " + mal.size);
}
@Override
public boolean add(T element) {
if (size >= array.length) {
// make a bigger array and copy over the elements
@SuppressWarnings("unchecked")
T[] bigger = (T[]) new Object[array.length * 2];
System.arraycopy(array, 0, bigger, 0, array.length);
array = bigger;
}
array[size] = element;
size++;
return true;
}
@Override
public void add(int index, T element) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
// add the element to get the resizing
add(element);
// shift the elements
for (int i=size-1; i>index; i--) {
array[i] = array[i-1];
}
// put the new one in the right place
array[index] = element;
}
@Override
public boolean addAll(Collection<? extends T> collection) {
boolean flag = true;
for (T element: collection) {
flag &= add(element);
}
return flag;
}
@Override
public boolean addAll(int index, Collection<? extends T> collection) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
// note: this version does not actually null out the references
// in the array, so it might delay garbage collection.
size = 0;
}
@Override
public boolean contains(Object obj) {
return indexOf(obj) != -1;
}
@Override
public boolean containsAll(Collection<?> collection) {
for (Object element: collection) {
if (!contains(element)) {
return false;
}
}
return true;
}
@Override
public T get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
return array[index];
}
@Override
public int indexOf(Object target) {
for (int i = 0; i<size; i++) {
if (equals(target, array[i])) {
return i;
}
}
return -1;
}
/** Checks whether an element of the array is the target.
*
* Handles the special case that the target is null.
*
* @param target
* @param object
*/
private boolean equals(Object target, Object element) {
if (target == null) {
return element == null;
} else {
return target.equals(element);
}
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public Iterator<T> iterator() {
// make a copy of the array
T[] copy = Arrays.copyOf(array, size);
// make a list and return an iterator
return Arrays.asList(copy).iterator();
}
@Override
public int lastIndexOf(Object target) {
// see notes on indexOf
for (int i = size-1; i>=0; i--) {
if (equals(target, array[i])) {
return i;
}
}
return -1;
}
@Override
public ListIterator<T> listIterator() {
// make a copy of the array
T[] copy = Arrays.copyOf(array, size);
// make a list and return an iterator
return Arrays.asList(copy).listIterator();
}
@Override
public ListIterator<T> listIterator(int index) {
// make a copy of the array
T[] copy = Arrays.copyOf(array, size);
// make a list and return an iterator
return Arrays.asList(copy).listIterator(index);
}
@Override
public boolean remove(Object obj) {
int index = indexOf(obj);
if (index == -1) {
return false;
}
remove(index);
return true;
}
@Override
public T remove(int index) {
T element = get(index);
for (int i=index; i<size-1; i++) {
array[i] = array[i+1];
}
size--;
return element;
}
@Override
public boolean removeAll(Collection<?> collection) {
boolean flag = true;
for (Object obj: collection) {
flag &= remove(obj);
}
return flag;
}
@Override
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public T set(int index, T element) {
// no need to check index; get will do it for us
T old = get(index);
array[index] = element;
return old;
}
@Override
public int size() {
return size;
}
@Override
public List<T> subList(int fromIndex, int toIndex) {
if (fromIndex < 0 || toIndex >= size || fromIndex > toIndex) {
throw new IndexOutOfBoundsException();
}
T[] copy = Arrays.copyOfRange(array, fromIndex, toIndex);
return Arrays.asList(copy);
}
@Override
public Object[] toArray() {
return Arrays.copyOf(array, size);
}
@Override
public <U> U[] toArray(U[] array) {
throw new UnsupportedOperationException();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.