forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.java
More file actions
201 lines (178 loc) · 4.62 KB
/
ArrayList.java
File metadata and controls
201 lines (178 loc) · 4.62 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
192
193
194
195
196
197
198
199
200
201
package com.examplehub.datastructures.array;
import java.util.StringJoiner;
public class ArrayList<E> {
/** Array to store elements. */
private E[] elements;
/** Default init capacity of this list. */
private static final int DEFAULT_CAPACITY = 10;
/** Max count number of elements in this list. */
private int capacity;
/** Count number of elements in this list. */
private int size;
/** Construct a list with a default capacity. */
public ArrayList() {
this(DEFAULT_CAPACITY);
}
/**
* Construct a list with a capacity.
*
* @param capacity the initial capacity.
*/
public ArrayList(int capacity) {
@SuppressWarnings("unchecked")
E[] tempArray = (E[]) new Object[capacity];
elements = tempArray;
this.capacity = capacity;
this.size = 0;
}
/**
* Returns a element at a given index.
*
* @param index the index of element to be returned.
* @return element at a given index.
*/
public E get(int index) {
checkIndex(index);
return elements[index];
}
/**
* Replace a element at given index in this list with special element.
*
* @param index the index to be updated.
* @param newValue the new value.
* @return original value at special index.
*/
public E set(int index, E newValue) {
E oldValue = get(index);
elements[index] = newValue;
return oldValue;
}
/**
* Add a item to the end of this list.
*
* @param item the item to be added.
* @return {@code true} if added successfully, otherwise {@code false}.
*/
public boolean add(E item) {
add(size, item);
return true;
}
/**
* Insert a item to special index of this list.
*
* @param index the special index of this list.
* @param item the item to be inserted.
* @throws IndexOutOfBoundsException if {@code index} is out of bounds.
*/
public void add(int index, E item) {
checkIndexForAdd(index);
growCapacityIfNeed();
if (size - index >= 0) System.arraycopy(elements, index, elements, index + 1, size - index);
elements[index] = item;
size++;
}
public void growCapacityIfNeed() {
if (capacity == size) {
int newCapacity = capacity + capacity / 2;
@SuppressWarnings("unchecked")
E[] newElements = (E[]) new Object[newCapacity];
for (int i = 0; i < size; ++i) {
newElements[i] = elements[i];
elements[i] = null; /* let gc do its work */
}
elements = newElements;
capacity = newCapacity;
}
}
/**
* Remove last item of this list.
*
* @return the last item of this list.
*/
public E remove() {
return remove(size - 1);
}
/**
* Remove the element at special index.
*
* @param index the index .
* @return element at special index.
* @throws IndexOutOfBoundsException if {@code index} is out of bounds.
*/
public E remove(int index) {
checkIndex(index);
E removed = elements[index];
if (size - 1 - index >= 0)
System.arraycopy(elements, index + 1, elements, index, size - 1 - index);
size--;
return removed;
}
/**
* Remove all given elements.
*
* @param target the target element to be removed.
* @return the number removed.
*/
public int remove(E target) {
int count = 0;
for (int i = 0; i < size; ++i) {
if (!elements[i].equals(target)) {
elements[count++] = elements[i];
}
}
int removedCount = size - count;
size = count;
return removedCount;
}
/** Clear all elements in this list. */
public void clear() {
for (int i = 0; i < size; ++i) {
elements[i] = null; /* let gc do its work */
}
size = 0;
}
/**
* Check index for add.
*
* @param index the index to be checked.
*/
private void checkIndexForAdd(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException(index + "");
}
}
/**
* Check index for removing or getting.
*
* @param index the index to be checked.
*/
private void checkIndex(int index) {
if (index < 0 || index > size - 1) {
throw new IndexOutOfBoundsException(index + "");
}
}
/**
* Return the number of this list.
*
* @return the number of this list.
*/
public int size() {
return size;
}
/**
* Check if this list contains no elements.
*
* @return {@code true} if this list contains no elements, otherwise {@code false}.
*/
public boolean isEmpty() {
return size == 0;
}
@Override
public String toString() {
StringJoiner joiner = new StringJoiner(", ", "[", "]");
for (int i = 0; i < size; ++i) {
joiner.add(elements[i].toString());
}
return joiner.toString();
}
}