-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyArrayList.java
145 lines (113 loc) · 3.67 KB
/
MyArrayList.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
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
import java.util.NoSuchElementException;
public class MyArrayList<E> implements MyCollection<E> {
private E[] items_array;
private int used_counter;
public MyArrayList() {
used_counter = 0;
items_array = (E[]) new Object[2];
}
public MyArrayList(int initialCapacity) {
used_counter = 0;
items_array = (E[]) new Object[initialCapacity];
}
@Override
public void clear() {
used_counter = 0;
}
@Override
public boolean isEmpty() {
return used_counter == 0;
}
private boolean isFull() {
return used_counter == items_array.length;
}
@Override
public int size() {
return used_counter;
}
private void resizeArray(int desired_size) { //O(n)
E[] replacement_array = (E[]) new Object[desired_size];
for (int i = 0; i < items_array.length && i < replacement_array.length; i++) {
replacement_array[i] = items_array[i];
}
items_array = replacement_array;
}
@Override
public void add(E item) { // --> O(1)
if (isFull()) { //0(1)
resizeArray(items_array.length * 2); // O(n)
}
items_array[used_counter] = item;
used_counter++;
}
private int findIndexof(E key) {
int index = 0;
boolean found = false;
while (index < used_counter) {
if (key == null) {
if (items_array[index] == null)
return index;
} else {
if (items_array[index].equals(key))
return index;
}
index++;
}
throw new NoSuchElementException("Item Not Found");
}
@Override
public boolean contains(E key) { //O(n)
try {
findIndexof(key);
return true;
} catch (NoSuchElementException nse) {
return false;
}
}
private void assertValidIndex(int index) throws IndexOutOfBoundsException {
if (index < 0 || index >= used_counter)
throw new IndexOutOfBoundsException("index too small or too large, asked for " +
index + " of " + used_counter + "possible items");
}
public E get(int index) throws IndexOutOfBoundsException {
assertValidIndex(index);
return items_array[index];
}
private void shiftItemsRight(int index) {
while (index < used_counter - 1) {
items_array[index] = items_array[index + 1];
index++;
}
used_counter--;
}
private boolean tooEmpty() {
return used_counter * 3 < items_array.length;
}
public E remove(int index) throws IndexOutOfBoundsException {
assertValidIndex(index);
E result = items_array[index];
shiftItemsRight(index); //process of removing and shifting items
if (tooEmpty())
resizeArray(items_array.length / 2);
return result;
}
@Override
public boolean remove(Object obj) {
if (obj == null) { //search for and remove first null
try {
int location = findIndexof(null);
remove(location);
return true;
} catch (NoSuchElementException nse) {
return false;
}
} else {
for (int index = 0; index < used_counter; index++)
if (obj.equals(items_array[index])) {
remove(index);
return true;
}
return false;
}
}
}