11/**
22 * A dynamic array implementation based on a fixed-size array.
33 *
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(T)</code>: O(1)</li>
10- * <li><code>insert(T, int)</code>: O(n)</li>
11- * <li><code>remove()</code>: O(1)</li>
12- * <li><code>remove(int)</code>: O(n)</li>
13- * <li><code>get</code>: O(1)</li>
14- * </ul>
4+ * <p>Operations with their time complexities are:
5+ *
6+ * <ul>
7+ * <li><code>shrinkCapacity()</code>: O(n)
8+ * <li><code>growCapacity()</code>: O(n)
9+ * <li><code>append(T)</code>: O(1)
10+ * <li><code>insert(T, int)</code>: O(n)
11+ * <li><code>remove()</code>: O(1)
12+ * <li><code>remove(int)</code>: O(n)
13+ * <li><code>get</code>: O(1)
14+ * </ul>
1515 */
1616public class DynamicArray <T > {
1717 Object [] array ; // The underlying fixed-size array
@@ -38,8 +38,7 @@ public int getCapacity() {
3838 public DynamicArray <T > shrinkCapacity () {
3939 Object [] newArray = new Object [this .length ];
4040
41- for (int i = 0 ; i < this .length ; i ++)
42- newArray [i ] = this .array [i ];
41+ for (int i = 0 ; i < this .length ; i ++) newArray [i ] = this .array [i ];
4342
4443 this .array = newArray ;
4544 return this ;
@@ -48,27 +47,23 @@ public DynamicArray<T> shrinkCapacity() {
4847 public DynamicArray <T > growCapacity () {
4948 Object [] newArray = new Object [this .array .length * 2 ];
5049
51- for (int i = 0 ; i < this .length ; i ++)
52- newArray [i ] = this .array [i ];
50+ for (int i = 0 ; i < this .length ; i ++) newArray [i ] = this .array [i ];
5351
5452 this .array = newArray ;
5553 return this ;
5654 }
5755
5856 public DynamicArray <T > append (T data ) {
59- if (this .length + 1 > this .array .length )
60- this .growCapacity ();
57+ if (this .length + 1 > this .array .length ) this .growCapacity ();
6158
6259 this .array [this .length ++] = data ;
6360 return this ;
6461 }
6562
6663 public DynamicArray <T > insert (T data , int index ) {
67- if (this .length + 1 > this .array .length )
68- this .growCapacity ();
64+ if (this .length + 1 > this .array .length ) this .growCapacity ();
6965
70- for (int i = this .length ++; i > index ; i --)
71- this .array [i ] = this .array [i - 1 ];
66+ for (int i = this .length ++; i > index ; i --) this .array [i ] = this .array [i - 1 ];
7267
7368 this .array [index ] = data ;
7469 return this ;
@@ -80,8 +75,7 @@ public DynamicArray<T> remove() {
8075 }
8176
8277 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+ for (int i = index + 1 ; i < this .length ; i ++) this .array [i - 1 ] = this .array [i ];
8579
8680 return this .remove ();
8781 }
0 commit comments