Repository navigation
Expand file tree
/
Copy pathArrayListImpl.java
More file actions
67 lines (59 loc) · 1.61 KB
/
Copy pathArrayListImpl.java
File metadata and controls
67 lines (59 loc) · 1.61 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
public class ArrayListImpl<T> implements Lista<T> {
private Object[] array;
private int count;
private static final int INITIAL_CAPACITY = 10;
public ArrayListImpl() {
array = new Object[INITIAL_CAPACITY];
count = 0;
}
@Override
public boolean isEmpty() {
return count == 0;
}
@Override
public int size() {
return count;
}
@Override
public void add(T element) {
if (count == array.length) {
expandCapacity();
}
array[count++] = element;
}
private void expandCapacity() {
Object[] newArray = new Object[array.length * 2];
System.arraycopy(array, 0, newArray, 0, array.length);
array = newArray;
}
@Override
public boolean remove(T element) {
for (int i = 0; i < count; i++) {
if (array[i].equals(element)) {
for (int j = i; j < count - 1; j++) {
array[j] = array[j + 1];
}
array[--count] = null;
return true;
}
}
return false;
}
@Override
public boolean contains(T element) {
for (int i = 0; i < count; i++) {
if (array[i].equals(element)) {
return true;
}
}
return false;
}
@Override
public void printList() {
System.out.print("ArrayList: ");
for (int i = 0; i < count; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}