-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomArrayList.java
More file actions
191 lines (161 loc) · 4.43 KB
/
Copy pathCustomArrayList.java
File metadata and controls
191 lines (161 loc) · 4.43 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
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
package CustomUtil.ArrayList;
import java.util.Arrays;
public class CustomArrayList<E> implements CustomArrayListInterface<E>{
private static final int DEFAULT_SIZE = 16;
private static final float INCREASE_SIZE_BY = 1.5f;
private Object[] data;
private int size;
public CustomArrayList() {
this(DEFAULT_SIZE);
}
public CustomArrayList(int capacity) {
this.data = new Object[capacity];
this.size = 0;
}
private void assignNewDataIfFull() {
if(size == data.length) {
int newCapacity = (int)(data.length * INCREASE_SIZE_BY);
Object[] newData = new Object[newCapacity];
System.arraycopy(data, 0, newData, 0, data.length);
data = newData;
}
}
@Override
public String toString() {
if(size == 0) {
return "[]";
}
StringBuilder res = new StringBuilder();
res.append("[ ");
for(int i = 0; i < size - 1; ++i) {
res.append(data[i]).append(", ");
}
res.append(data[size - 1]).append(" ]");
return res.toString();
}
private void isIndexInBound(int index) throws IndexOutOfBoundsException {
if(index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index out of bound");
}
}
/*
* Adds element into the ArrayList
* With O(1) TC
* */
public void add(E element) {
assignNewDataIfFull();
data[size++] = element;
}
/*
* Adds element into the ArrayList
* with the specified index
* With O(n) TC
* */
public void add(int index, E element) {
isIndexInBound(index);
assignNewDataIfFull();
for(int i = size; i > index; --i) {
data[i] = data[i - 1];
}
data[index] = element;
size++;
}
/*
* Retrieves element with the specified index
* With O(1) TC
* */
@SuppressWarnings("unchecked")
public E get(int index) {
isIndexInBound(index);
return (E)(data[index]);
}
/*
* Inserts an element into the ArrayList with the specified index
* If an element is already present in that index
* the element will get replaced with the specified element
* With O(1) TC
* */
public void set(int index, E element) {
isIndexInBound(index);
data[index] = element;
}
/*
* Removes the element from the specified index
* with O(n) TC
* */
@SuppressWarnings("unchecked")
public E remove(int index) {
isIndexInBound(index);
E element = (E)(data[index]);
for(int i = index; i < size - 1; ++i) {
E temp = (E)(data[i]);
data[i] = data[i+1];
data[i+1] = temp;
}
size--;
return element;
}
/*
* Returns the size of the ArrayList
* With O(1) TC
* */
public int size() {
return size;
}
/*
* Returns a boolean value
* True if ArrayList is empty, otherwise false
* With O(1) TC
* */
public boolean isEmpty() {
return size == 0;
}
/*
* Returns a boolean value
* True if ArrayList contains the specified element, otherwise false
* With O(n) TC
* */
public boolean contains(E element) {
for(int i = 0; i < size; ++i) {
if(data[i] == element) {
return true;
}
}
return false;
}
/*
* Returns the index of the specified element
* If multiple occurrences of an element is present
* Index of the first occurrence of that element will be returned
* With O(n) TC
* */
public int indexOf(E element) {
for(int i = 0; i < size; ++i) {
if(data[i] == element) {
return i;
}
}
return -1;
}
/*
* Removes every element present in the ArrayList
* With O(n) TC
* */
public void clear() {
Arrays.fill(data, null);
size = 0;
}
/*
* Pre-allocate ArrayList size with the provided capacity
* With O(n) TC
* */
public void ensureCapacity(int newCapacity) {
if(newCapacity <= data.length) {
return;
}
int maxCapacity = Math.max(newCapacity, (int)(data.length * INCREASE_SIZE_BY));
Object[] newData = new Object[maxCapacity];
if (size >= 0) System.arraycopy(data, 0, newData, 0, size);
data = newData;
}
}