A dynamic array is a data structure that allows for the creation of a random access, variable-size list where elements can be added or removed. It is also known as a growable array, resizable array, dynamic table, mutable array, or array list. Dynamic arrays are supplied with standard libraries in many modern mainstream programming languages.
Dynamic arrays overcome the limitation of static arrays, which have a fixed capacity that needs to be specified at allocation. 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.
Dynamic arrays benefit from many of the advantages of arrays, including good locality of reference and data cache utilization, compactness (low memory use), and random access. They usually have only a small fixed additional overhead for storing information about the size and capacity.
In Java, dynamic arrays are called ArrayLists. When you allocate a dynamic array, your dynamic array implementation makes an underlying fixed-size array. The starting size depends on the implementation, and when you append items to the dynamic array, the underlying arrays length may be larger than the dynamic arrays length. The dynamic array stores an endIndex to keep track of where the dynamic array ends and the extra capacity begins. When you try to append an item but the arrays capacity is already full, dynamic arrays automatically make a new, bigger underlying array, usually twice as big.
In C++, dynamic arrays are created by allocating an array of fixed-size, typically larger than the number of elements immediately required, and then dynamically allocating that array. There is no way to chop off the excess indexes from the dynamic array, so deleting the array at the end is the only choice.