-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayGenericList.java
More file actions
150 lines (130 loc) · 3.83 KB
/
ArrayGenericList.java
File metadata and controls
150 lines (130 loc) · 3.83 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
148
149
150
package arraylist;
import genericlist.AbstractGenericList;
import genericlist.StepCounter;
import genericlist.GenericList;
import java.util.Arrays;
/*
* creates Specific list from given parameters
*/
public class ArrayGenericList<E> extends AbstractGenericList<E> {
/*represents size of elements*/
private static final int defaultvalue = 10;
/*represents unspecified elements*/
private E[] elements;
/** Default constructor */
/*
* Creates list from counter
* fills in with unspecified elements
* @param counter - size of list
*/
@SuppressWarnings("unchecked")
public ArrayGenericList(StepCounter counter) {
super(counter);
elements = (E[]) new Object[defaultvalue];
}
/*
* Creates list from counter,
* fills in with specified number of elements
* @param capacity -amount of elements
* @param counter - size of list
*/
@SuppressWarnings("unchecked")
public ArrayGenericList(int capacity, StepCounter counter) {
super(counter);
if (capacity <= 0) throw new IllegalArgumentException("Capacity must be > 0");
elements = (E[]) new Object[capacity];
}
/*
* ensures there is space in a given list
* @param none
* @return none
*/
@SuppressWarnings("unchecked")
private void ensureCapacity() {
if (size >= elements.length) {
int newCap = elements.length * 2;
elements = Arrays.copyOf(elements, newCap);
counter.add(newCap);
}
}
@Override
public void add(E e) {
ensureCapacity();
elements[size++] = e;
counter.inc();
}
@Override
public void add(int index, E e) {
checkPositionIndex(index);
ensureCapacity();
for (int i = size; i > index; i--) {
elements[i] = elements[i - 1];
counter.inc();
}
elements[index] = e;
size++;
counter.inc();
}
@Override
public E get(int index) {
checkElementIndex(index);
counter.inc();
return elements[index];
}
public E set(int index, E e) {
checkElementIndex(index);
E old = elements[index];
elements[index] = e;
counter.inc();
return old;
}
@Override
public E remove(int index) {
checkElementIndex(index);
E old = elements[index];
for (int i = index; i < size - 1; i++) {
elements[i] = elements[i + 1];
counter.inc();
}
elements[--size] = null; // clear reference for GC
counter.inc();
return old;
}
@SuppressWarnings("unchecked")
public boolean remove(Object o) {
int i = contains((E) o); //unsafe type, resolved by suppressing warning.
if (i >= 0) {
remove(i);
return true;
}
return false;
}
@Override
public void clear() {
for (int i = 0; i < size; i++) elements[i] = null;
counter.inc();
size = 0;
}
@Override
public int contains(E element) { //returns index
for (int i = 0; i < size; i++) {
counter.inc();
if (element == null ? elements[i] == null : element.equals(elements[i])) return i;
}
return -1;}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < size; i++) {
sb.append(elements[i]);
if (i < size - 1) sb.append(", ");
}
sb.append("]");
return sb.toString();
}
@Override
public GenericList<E> newList() {
// TODO Auto-generated method stub
return null;
}
}