|
1 | | -@SuppressWarnings("unchecked") |
| 1 | +/** |
| 2 | + * A dynamic array implementation based on a fixed-size array. |
| 3 | + * |
| 4 | + * <p> |
| 5 | + * Operations with their time complexities are: |
| 6 | + * <ul> |
| 7 | + * <li><code>shrinkCapacity</code>: O(n)</li> |
| 8 | + * <li><code>growCapacity</code>: O(n)</li> |
| 9 | + * <li><code>append</code>: O(1)</li> |
| 10 | + * <li><code>insert</code>: O(n)</li> |
| 11 | + * <li><code>remove</code> (at last element): O(1)</li> |
| 12 | + * <li><code>remove</code> (at specified index): O(n)</li> |
| 13 | + * <li><code>get</code>: O(1)</li> |
| 14 | + * </ul> |
| 15 | + */ |
2 | 16 | public class DynamicArray<T> { |
3 | | - private T[] array; // The underlying fixed-size array |
4 | | - private int size = 0; // The number of elements inside this dynamic array |
| 17 | + private Object[] array; // The underlying fixed-size array |
| 18 | + private int length; // The number of elements in this array |
5 | 19 |
|
6 | | - /** Initializes a new dynamic array with an initial capacity of 10. */ |
7 | 20 | public DynamicArray() { |
8 | | - this.array = (T[]) new Object[10]; |
| 21 | + this.array = new Object[10]; |
| 22 | + this.length = 0; |
9 | 23 | } |
10 | 24 |
|
11 | | - /** |
12 | | - * Initializes a new dynamic array with the given initial capacity. |
13 | | - * |
14 | | - * @param initialCapacity the initial capacity of the new dynamic array |
15 | | - */ |
16 | 25 | public DynamicArray(int initialCapacity) { |
17 | | - this.array = (T[]) new Object[initialCapacity]; |
| 26 | + this.array = new Object[initialCapacity]; |
| 27 | + this.length = 0; |
18 | 28 | } |
19 | 29 |
|
20 | | - /** |
21 | | - * Returns the number of elements in this dynamic array. |
22 | | - * |
23 | | - * @return the number of elements in this dynamic array. |
24 | | - */ |
25 | | - public int getSize() { |
26 | | - return this.size; |
| 30 | + public int getLength() { |
| 31 | + return this.length; |
27 | 32 | } |
28 | 33 |
|
29 | | - /** Shrinks the capacity of the underlying fixed-size array to free unused memory. */ |
30 | | - public void shrinkSize() { |
31 | | - T[] newArray = (T[]) new Object[this.size]; |
32 | | - for (int i = 0; i < this.size; i++) newArray[i] = this.array[i]; |
| 34 | + public int getCapacity() { |
| 35 | + return this.array.length; |
| 36 | + } |
| 37 | + |
| 38 | + public DynamicArray<T> shrinkCapacity() { |
| 39 | + Object[] newArray = new Object[this.length]; |
| 40 | + |
| 41 | + for (int i = 0; i < this.length; i++) |
| 42 | + newArray[i] = this.array[i]; |
| 43 | + |
33 | 44 | this.array = newArray; |
| 45 | + return this; |
34 | 46 | } |
35 | 47 |
|
36 | | - /** Doubles the capacity of the underlying fixed-size array. */ |
37 | | - public void growSize() { |
38 | | - T[] newArray = (T[]) new Object[this.array.length * 2]; |
39 | | - for (int i = 0; i < this.size; i++) newArray[i] = this.array[i]; |
| 48 | + public DynamicArray<T> growCapacity() { |
| 49 | + Object[] newArray = new Object[this.array.length * 2]; |
| 50 | + |
| 51 | + for (int i = 0; i < this.length; i++) |
| 52 | + newArray[i] = this.array[i]; |
| 53 | + |
40 | 54 | this.array = newArray; |
| 55 | + return this; |
41 | 56 | } |
42 | 57 |
|
43 | | - /** |
44 | | - * Adds a new element to the end of this dynamic array. |
45 | | - * |
46 | | - * @param e the new element to be added to the end |
47 | | - */ |
48 | | - public void add(T e) { |
49 | | - if (this.size + 1 > this.array.length) this.growSize(); |
50 | | - this.array[this.size++] = e; |
| 58 | + public DynamicArray<T> append(T data) { |
| 59 | + if (this.length + 1 > this.array.length) |
| 60 | + this.growCapacity(); |
| 61 | + |
| 62 | + this.array[this.length++] = data; |
| 63 | + return this; |
51 | 64 | } |
52 | 65 |
|
53 | | - /** |
54 | | - * Adds a new element at the specified index in this dynamic array. |
55 | | - * |
56 | | - * @param e the new element to be added at the specified index |
57 | | - * @param index the index to add the new element at |
58 | | - */ |
59 | | - public void add(T e, int index) { |
60 | | - if (this.size + 1 > this.array.length) this.growSize(); |
61 | | - for (int i = this.size++; i > index; i--) this.array[i] = this.array[i - 1]; |
62 | | - this.array[index] = e; |
| 66 | + public DynamicArray<T> insert(T data, int index) { |
| 67 | + if (this.length + 1 > this.array.length) |
| 68 | + this.growCapacity(); |
| 69 | + |
| 70 | + for (int i = this.length++; i > index; i--) |
| 71 | + this.array[i] = this.array[i - 1]; |
| 72 | + |
| 73 | + this.array[index] = data; |
| 74 | + return this; |
63 | 75 | } |
64 | 76 |
|
65 | | - /** Removes the last element in this dynamic array. */ |
66 | | - public void remove() { |
67 | | - this.array[--this.size] = null; |
| 77 | + public DynamicArray<T> remove() { |
| 78 | + this.array[--this.length] = null; |
| 79 | + return this; |
68 | 80 | } |
69 | 81 |
|
70 | | - /** |
71 | | - * Removes the element at the specified index in this dynamic array. |
72 | | - * |
73 | | - * @param index the index of the element to be removed |
74 | | - */ |
75 | | - public void removeAt(int index) { |
76 | | - // Shift to the right starting at the specified index |
77 | | - for (int i = index; i < this.size - 1; i++) this.array[i] = this.array[i + 1]; |
| 82 | + public DynamicArray<T> remove(int index) { |
| 83 | + for (int i = index + 1; i < this.length; i++) |
| 84 | + this.array[i - 1] = this.array[i]; |
78 | 85 |
|
79 | | - this.remove(); |
| 86 | + return this.remove(); |
80 | 87 | } |
81 | 88 |
|
82 | | - /** |
83 | | - * Returns the element at the specified index in this dynamic array. |
84 | | - * |
85 | | - * @param index the index of the element to be returned |
86 | | - */ |
87 | | - public T getAt(int index) { |
| 89 | + @SuppressWarnings("unchecked") |
| 90 | + public T get(int index) { |
88 | 91 | return (T) this.array[index]; |
89 | 92 | } |
90 | | - |
91 | | - /** |
92 | | - * Prints elements in the specified dynamic array line-by-line. |
93 | | - * |
94 | | - * @param array the dynamic array to be printed |
95 | | - */ |
96 | | - public static void printDynamicArray(DynamicArray array) { |
97 | | - for (int i = 0; i < array.getSize(); i++) |
98 | | - System.out.println(array.getAt(i)); |
99 | | - } |
100 | 93 | } |
0 commit comments