A simple dynamic array can be constructed by allocating an array of fixed-size, typically larger than the number of elements immediately required. The elements of the dynamic array are stored contiguously at the start of the underlying array, and the remaining positions towards the end of the underlying array are reserved, or unused. Elements can be added at the end of a dynamic array in constant time by using the reserved space until this space is completely consumed.
When all space is consumed, and an additional element is to be added, the underlying fixed-sized array needs to be increased in size. Typically resizing is expensive because you have to allocate a bigger array and copy over all of the elements from the array you have overgrow before we can finally append our item.
Approach: When we enter an element in array but array is full then you create a function, this function creates a new array double size or as you wish and copy all element from the previous array to a new array and return this new array. Also, we can reduce the size of the array. and add an element at a given position, remove the element at the end default and at the position also.
Key Features of Dynamic Array
Add Element: Add element at the end if the array size is not enough then extend the size of the array and add an element at the end of the original array as well as given index. Doing all that copying takes O(n) time, where n is the number of elements in our array. That’s an expensive cost for an append. In a fixed-length array, appends only take O(1) time.
But appends take O(n) time only when we insert into a full array. And that is pretty rare, especially if we double the size of the array every time we run out of space. So in most cases appending is still O(1) time, and sometimes it’s O(n) time.
In dynamic array you can create fixed-size array when required added some more element in array then use this approach:
Delete Element: Delete an element from array, default remove() method delete an element from end, simply store zero at last index and you also delete element at specific index by calling removeAt(i) method where i is index. removeAt(i) method shift all right element in the left side from the given index.
Resize of Array Size: When the array has null/zero data(exclude add by you) at the right side of the array whose take unrequited memory, the method srinkSize() free extra memory. When all space is consumed, and an additional element is to be added, then the underlying fixed-size array needs to increase size. Typically resizing is expensive because you have to allocate a bigger array and copy over all of the elements from the array you have overgrow before we can finally append our item.
Simple code for dynamic array. In below code then the array will become full of size we copy all element to new double size array(variable size array).sample code is below
Elements of array: 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 Size of array: 16 No of elements in array: 9 Elements of array after shrinkSize of array: 1 2 3 4 5 6 7 8 9 Size of array: 9 No of elements in array: 9 Elements of array after add an element at index 1: 1 22 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 Size of array: 18 No of elements in array: 10 Elements of array after delete last element: 1 22 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0 0 Size of array: 18 No of elements in array: 9 Elements of array after delete element at index 1: 1 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0 0 0 Size of array: 18 No of elements in array: 8
- Proof of Work (PoW) Consensus
- How does Duff's Device work?
- Dynamic Frequency Scaling and Dynamic Voltage Scaling
- Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
- Merge k sorted arrays | Set 2 (Different Sized Arrays)
- Find sub-arrays from given two arrays such that they have equal sum
- Implementation of Dynamic Array in Python
- Static vs Dynamic Binding in Java
- Sorting a dynamic 2-dimensional array of Strings
- Algorithm for Dynamic Time out timer Calculation
- Dynamic Method Dispatch or Runtime Polymorphism in Java
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Java.util.Arrays.parallelSetAll(), Arrays.setAll() in Java
- Maximum OR sum of sub-arrays of two different arrays
- Arrays in C/C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.