Dynamic Array in Java
An array is a fixed size, homogeneous data structure. The limitation of arrays is that they're fixed in size. It means that we must specify the number of elements while declaring the array. Here a question arises that what if we want to insert an element and there is no more space is left for the new element? Here, the concept of dynamic array comes into existence. It expends the size of the array dynamically.
In this section, we will understand what is a dynamic array, features of the dynamic array, how to resize a dynamic array, and how to implement dynamic array in Java.
What is a dynamic array?
The dynamic array is a variable size list data structure. It grows automatically when we try to insert an element if there is no more space left for the new element. It allows us to add and remove elements. It allocates memory at run time using the heap. It can change its size during run time.
In Java, ArrayList is a resizable implementation. It implements the List interface and provides all methods related to the list operations. The strength of the dynamic array is:
Working of Dynamic Array
In the dynamic array, the elements are stored contiguously from the starting of the array and the remaining space remains unused. We can add the elements until the reserved spaced is completely consumed. When the reserved space is consumed and required to add some elements. In such a case, the fixed-sized array needs to be increased in size. Note that before appending the element, we allocate a bigger array, copy the elements from the array, and return the newly created array.
Another way to add an element is that first, create a function that creates a new array of double size, copies all the elements from the old array, and returns the new array. Similarly, we can also shrink the size of the dynamic array.
Size vs. Capacity
The initialization of a dynamic array creates a fixed-size array. In the following figure, the array implementation has 10 indices. We have added five elements to the array. Now, the underlying array has a length of five. Therefore, the length of the dynamic array size is 5 and its capacity is 10. The dynamic array keeps track of the endpoint.
Features of Dynamic Array
In Java, the dynamic array has three key features: Add element, delete an element, and resize an array.
Add Element in a Dynamic Array
In the dynamic array, we can create a fixed-size array if we required to add some more elements in the array. Usually, it creates a new array of double size. After that, it copies all the elements to the newly created array. We use the following approach:
Delete an Element from a Dynamic Array
If we want to remove an element from the array at the specified index, we use the removeAt(i) method. The method parses the index number of that element which we want to delete. After deleting the element, it shifts the remaining elements (elements that are right to the deleted element) to the left from the specified index number. We also use the remove() method that deletes an element from the end of the array. After shifting the elements, it stores 0 at the palace of the last element. Let's understand it through an example, as we have shown in the following figure.
Resizing a Dynamic Array in Java
We need to resize an array in two scenarios if:
In the first case, we use the srinkSize() method to resize the array. It reduces the size of the array. It free up the extra or unused memory. In the second case, we use the growSize() method to resize the array. It increases the size of the array.
It is an expensive operation because it requires a bigger array and copies all the elements from the previous array after that return the new array.
Suppose in the above array, it is required to add six more elements and, in the array, no more memory is left to store elements. In such cases, we grow the array using the growSize() method.
Initialize a Dynamic Array
The initialization of the dynamic array is the same as the static array. Consider the following Java program that initializes a dynamic array.
Elements of Array are: 34 90 12 22 9 27
Let's implement the operations in a Java program that we have discussed above.
Let's shrink the array, delete the last element, and a specified element from the array.