-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicArray.java
More file actions
194 lines (155 loc) · 5.79 KB
/
DynamicArray.java
File metadata and controls
194 lines (155 loc) · 5.79 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
import java.util.Arrays;
import java.util.Iterator;
/**
* A generic dynamic array implementation that automatically resizes as elements are added or removed.
* Its functionality is similar to that of the {@code ArrayList} class.
*
* @param <T> The type of elements stored in the dynamic array
* @author Taskin Saadman
* @see java.util.ArrayList
* @see Iterable
*/
@SuppressWarnings("unchecked")
public class DynamicArray<T> implements Iterable<T> {
private T[] array; //internal static array storing data
private int size; //no. of elements inside
/**
* Creates a dynamic array with an initial capacity of 10.
*/
public DynamicArray() {
array = (T[]) new Object[10]; //typecasting warning suppressed
size = 0;
}
/**
* Returns the number of elements currently stored in the dynamic array.
*
* @return the current size of the array
*/
public int size() {
return size;
}
/**
* Returns the element in the specified index
*
* @param index the specified index
* @return element the element at the specified index
* @throws IndexOutOfBoundsException
*/
public T get(int index) throws IndexOutOfBoundsException {
if (!(index >= 0 && index < size)) throw new IndexOutOfBoundsException("Index " + index + " is out of bounds.");
return array[index];
}
/**
* Set an element at a specified index
*
* @param index the specified index
* @param element the element to be set
* @throws IndexOutOfBoundsException
* @throws NullPointerException
*/
public void set(int index, T element) {
if (!(index >= 0 && index < size)) throw new IndexOutOfBoundsException("Index " + index + " is out of bounds.");
if (element == null) throw new NullPointerException("Dynamic array can't contain empty reference types");
array[index] = element;
}
/**
* Adds an element to the end of the dynamic array.
* If the array is full, it doubles the capacity before adding.
*
* @param element The element to add
*/
public void add(T element) {
if (size == array.length) array = Arrays.copyOf(array, array.length * 2); //if no more capacity, double the array
array[size++] = element; //increment size after adding new element
}
/**
* Inserts an element to a specified index and shifts rest of the
* elements to the right of the dynamic array.
*
* @param index The position where the element should be inserted
* @param element The element to add
* @throws IndexOutOfBoundsException if index is negative or greater than size
* @throw NullPointerException if {@code null} was passed as element to be inserted
*/
public void insert(int index, T element) {
//insertion is allowed at the very end as well
if (!(index >= 0 && index <= size)) throw new IndexOutOfBoundsException("Index " + index + " is out of bounds.");
if (element == null) throw new NullPointerException("Dynamic array can't contain empty reference types");
if (size == array.length) array = Arrays.copyOf(array, array.length * 2);
for(int i = size - 1; i >= index; i--) { //shift everything to the right from specified index
array[i + 1] = array[i];
}
array[index] = element; //insert element and increment size
size++;
}
/**
* Removes element at specified index and left shift the rest of the array.
*
* @param index index from which element is to be popped
* @return element the element which was popped
* @throws IndexOutOfBoundsException if out of bounds index was passed
*/
public T pop(int index) throws IndexOutOfBoundsException {
if (!(index >= 0 && index < size)) throw new IndexOutOfBoundsException();
T element = array[index];
for(int i = index; i < size; i++) { //left-shift
array[i] = array[i+1];
}
size--; //decrement size and return popped element
return element;
}
/**
* Removes the last element of the dynamic array and returns it
* @return element the last element of the dynamic array
*/
public T pop() {
T element = array[size - 1];
array[size - 1] = null;
size--;
return element;
}
/**
* Checks whether an element exists inside the dynamic array.
* @param element element to be found
* @return boolean true or false
* @throws NullPointerException if {@code null} was passed as argument
*/
public boolean contains(T element) throws NullPointerException {
if (element == null) throw new NullPointerException("Dynamic array cannot contain empty reference types!");
for(int i = 0; i < size; i++) {
if (array[i].equals(element)) return true;
}
return false;
}
/**
* Substitute old internal static array with new and reset size.
*/
public void clear() {
array = (T[]) new Object[10];
size = 0;
}
/**
* Returns a string representation of the {@code DynamicArray} object.
* @return String
*/
public String toString() {
return Arrays.toString(Arrays.copyOf(array,size));
}
/**
* Method overridden from Iterable<T> interface.
* Useful to iterate through the dynamic array using for-each loop.
* @return an iterator over elements of type {@code T}
*/
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private int current = 0;
public boolean hasNext() {
return current < size;
}
public T next() {
return array[current++];
}
};
}
}