-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathMyArrayList.java
More file actions
154 lines (136 loc) · 4.55 KB
/
MyArrayList.java
File metadata and controls
154 lines (136 loc) · 4.55 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
import java.util.Arrays;
public class MyArrayList<E> {
private int size;
private static final int DEFAULT_CAPACITY = 10;
private Object[] inputArray;
public MyArrayList() {
this(DEFAULT_CAPACITY);
}
public MyArrayList(int capacity) {
this.inputArray = new Object[capacity];
this.size = 0;
}
/**
* @return current number of elements in this list
*/
public int size() {
return size;
}
/**
* @return true if this list contains no elements
*/
public Boolean isEmpty() {
if (this.size == 0) {
return true;
}
return false;
}
/**
* Increases the capacity of the ArrayList instance, if necessary,
* to ensure that it can hold at least the number of elements
*/
private void ensureCapacity() {
if (size >= this.inputArray.length) {
int newCapacity = this.inputArray.length + DEFAULT_CAPACITY;
this.inputArray = Arrays.copyOf(this.inputArray, newCapacity);
}
}
/**
* Appends the specified element to the end of this list
*
* @param addMe element to be appended to this list
* @return true if appended and false if not appended
*/
public Boolean add(E addMe) {
ensureCapacity();
this.inputArray[size++] = addMe;
return true;
}
/**
* Inserts the specified element at the specified position in this list.
* Shifts the element currently at that position(if any) and any subsequent
* elements to the right(adds 1 to the other indices).
*
* @param index at which specified element is to be inserted
* @param addMe element to be inserted to this list
* @return true if appended and false if not appended
*/
public Boolean add(int index, E addMe) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index is too large or negative");
}
int length = size() - index;
ensureCapacity();
System.arraycopy(this.inputArray, index, this.inputArray, index + 1, length);
this.inputArray[index] = addMe;
return true;
}
/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left(subtracts one from their indices).
*
* @param index of element to be removed
* @return the element that was removed from the list
*/
@SuppressWarnings("unchecked")
public E remove(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index is too large or negative");
}
E storePreviousElement = (E) this.inputArray[index];
int length = size() - index;
if (length > 0) {
System.arraycopy(this.inputArray, index + 1, this.inputArray, index, length);
this.inputArray[size--] = null;
}
return storePreviousElement;
}
/**
* Returns the element at the specified position in this list
*
* @param index of element to return
* @return element at the specified position in this list
*/
@SuppressWarnings("unchecked")
public E get(Integer index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index is too large or negative");
}
return (E) this.inputArray[index];
}
/**
* Removes all of the elements from this list
* The list will be empty after this call returns.
*/
public void clear() {
this.inputArray = new Object[DEFAULT_CAPACITY];
this.size = 0;
}
/**
* @param element to be checked
* @return true if list contains the specified element.
*/
public Boolean contains(E element) {
for (Object obj : this.inputArray) {
if (obj != null && obj.equals(element))
return true;
}
return false;
}
/**
* Replaces the element at the specified position in this list with the specified element.
*
* @param index of the element to replace
* @param element to be stored at the specified position
* @return the element previously at the specified position
*/
@SuppressWarnings("unchecked")
public E set(int index, E element) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index is too large or negative");
}
E storePreviousElement = (E) this.inputArray[index];
this.inputArray[index] = element;
return storePreviousElement;
}
}