An array is a data structure that stores elements of the same type in one contiguous block of memory, so any element is read by its index in O(1) time, while inserting or deleting anywhere but at the end shifts the elements after that position. A dynamic array also resizes itself when a value is added while it is full, by copying its elements into a larger array. This page explains how it works, with step-by-step animations of insertion, overwriting, deletion and dynamic array resizing from the VisiGrab app.
// how arrays work
An array is a data structure that stores elements of one type in one contiguous block of memory. Each element has an integer index, its position. All elements share one type, so an array is homogeneous. They form a sequence, so it is a linear data structure; trees, hash tables and graphs are non-linear. The app shows this split, then each part, step by step.
Square brackets select an element: A[2] is the element at index 2. Most languages use zero-based indexing: the first element is A[0], and the last of n elements is A[n − 1]. The index is an offset from the start of the array, so the first element is at offset 0. Fortran, MATLAB, Lua, R and Julia count from 1. Arrays give random access: any element is read directly, in any order, without passing the ones before it.
Assigning values to the elements is initialization. Until then, an array holds defaults, such as null or 0 in Java, or leftover values in a local C array. With only some values assigned, it is partially initialized.
An array takes one contiguous block of memory, starting at the base address. In an array of 4-byte integers, each element starts 4 bytes after the one before.
So the address of any element can be computed:
address of A[i] = base_address + i × element_size
With the base address 0x00001000 (0x…1000 in the figure), A[2] is at 0x00001000 + 2 × 4 = 0x00001008. One multiplication and one addition reach any element, so access by index takes O(1) time. Contiguous memory also gives cache locality: reading one element loads its neighbors into the cache.
A static array, or fixed-size array, gets its size at creation and keeps it. A dynamic array, or resizable array, grows as values are added. A static array suits a known count, such as the app's eight planets. For chat messages, too small an array goes out of bounds and too large a one wastes memory; the app shows both, step by step.
Note
In C and C++, "dynamic array" can also mean a heap array from new[] or malloc, which does not grow by itself.
The valid indices of an array of size n are 0 to n − 1; any other index is out of bounds. Java throws ArrayIndexOutOfBoundsException, and Python raises IndexError: list index out of range. In Python, a[-1] is valid: it is the last element. C and C++ do not check plain arrays: the access is undefined behavior.
A dynamic array usually stores its values in a static array, the underlying array, with spare slots. Its size is the number of elements in use, and its capacity the number of slots: the logical and the physical size.
Appending to a full dynamic array makes it resize: it allocates a new array a fixed factor larger, often twice as large, copies every element over, drops the old one and adds the value. In the player, e fills the last of five slots, and f makes the array resize: five copies into an array of capacity 10, then f goes in, and the size is 6.
In the worst case, one append costs O(n), because a resize copies all n elements. Doubling from capacity 1, an array of n elements has resized only about log2 n times: 8 doublings reach 256. When n is a power of 2, the copies cost 1 + 2 + 4 + … + n/2 = n − 1; for any n they cost less than 2n. So n appends cost O(n) in total, or O(1) each. This is the amortized cost: the average per operation over a worst-case sequence. Any growth factor above 1 gives it; adding a fixed number of slots does not. The app plots both costs on one graph.
To insert at index i, every element from index i to the last one moves one slot right, last first: one move per element. A static array needs free space for the shift; a full one cannot take the value, but it can still overwrite one. A full dynamic array resizes first.
Inserting at the beginning moves every element. In the player, 99 goes into index 0 of 8, 5, 0, 1, 4, 9: the six values shift one slot right, last first, so none is overwritten, and then 99 is written.
Inserting at the end moves nothing: O(1), amortized for a dynamic array. The app also shows inserts and deletes at an arbitrary index.
A full static array cannot take an insert, but it can still overwrite: A[i] = v replaces the value at index i in O(1), moves nothing and keeps the size, but the old value is lost. This is how a full array in Java or C++ changes its contents.
Note
A Python list is dynamic and never full, but a[i] = v replaces a value there too.
Here the array A, full with 8, 5, 0, 1, 4, 9 and 3, gets A[3] = 7: the 1 at index 3 is replaced.
Deleting leaves a gap, and no gaps are allowed, so every later element moves one slot left, first to last. In the player, 99 is deleted from index 0 of 99, 8, 5, 0, 1, 4, 9, and the other six values each move one slot left.
Deleting the last element moves nothing: O(1). A dynamic array may also shrink when it gets sparse. Halving the capacity at exactly half full can thrash: alternating appends and deletes then resize every time. The usual rule halves it at a quarter full, which keeps deletion O(1) amortized. Java's ArrayList and C++'s std::vector never shrink on their own.
A dynamic array is an array that resizes itself as values are added. When its underlying array is full, an append allocates a larger one, twice as large in the app, and copies every value over. Resizes are rare enough that an append costs O(1) amortized.
Every element must first move one slot right to free index 0, so with n elements the insert makes n moves: O(n). The second player shows all six moves.
| Operation | Static array | Dynamic array |
|---|---|---|
| Access by index | O(1) | O(1) |
| Overwrite by index | O(1) | O(1) |
| Search, unsorted | O(n) | O(n) |
| Search, sorted (binary search) | O(log n) | O(log n) |
| Insert at the end | O(1) if free space is available | O(1) amortized, O(n) worst case |
| Delete at the end | O(1) | O(1), amortized if it shrinks |
| Insert at the beginning or middle | O(n) if free space is available | O(n) |
| Delete at the beginning or middle | O(n) | O(n) |
Space is O(n); right after doubling, about half the slots are free.
Python's list, Java's ArrayList and C++'s std::vector are dynamic arrays. Java's int[] and C++'s std::array are fixed-size. Growth factors differ: about 1.125 in CPython, 1.5 in Java, and 2 in GCC and Clang but 1.5 in MSVC.
The full source code is on GitHub, in Python, Java and C++.
| Property | Array | Linked list |
|---|---|---|
| Access by index | O(1) | O(n) |
| Insert or delete at the front | O(n) | O(1) |
| Memory | The values, plus any spare capacity | Each value with one or two links |
| Layout | Contiguous, cache-friendly | Nodes scattered in memory |
Arrays suit access by index and scans; linked lists suit frequent inserts and deletes at the front. In practice arrays often win even for inserts in the middle: shifting contiguous memory is faster than following links.
Note
In C and C++ a link is a pointer; in Java and Python it is a reference.
A two-dimensional (2D) array is a grid, read as A[i][j]. C and C++ store it in row-major order, row after row: with c columns, A[i][j] is at base_address + (i × c + j) × element_size. Fortran and MATLAB use column-major order. In Java, a 2D array is an array of rows, which can differ in length: a jagged array. A graph's adjacency matrix is a two-dimensional array.
O(log n).Advantages: O(1) access by index, no per-element overhead and good cache use. Disadvantages: a static array has a fixed size, and inserts and deletes cost O(n) anywhere but the end.
6:27
Video
Arrays Explained | Neo & Byte Ep. 0
Indexing from 0, static and dynamic arrays, and why inserting at the beginning is slow.
Watch on YouTube
// in the app
In VisiGrab the array is a 58-slide show, from what an array is to dynamic arrays, played one step at a time.
Why a static array suits the eight planets but not a chat: an index-out-of-bounds error, then a 1,000-slot array, most of it unused.
One graph shows the worst case, resizes growing as log n and the amortized O(1) line. Then deleting at the end, with and without halving.
Step through every slide and step back, or jump to any slide from the list. The show reopens where you left it.
Two tabs: Info, with the key characteristics, and Efficiency, with a complexity table for static and dynamic arrays. A tip shows an array in memory, byte by byte.
Static arrays in Java and C++, Python's list, Java's ArrayList and C++'s std::vector, and a dynamic array built from scratch in Java and C++, explained part by part.
Ten questions to check what you have learned before moving on.
// build your own
Download VisiGrab and step through indexing, resizing, shifting and amortized analysis yourself.
The array is one of 30+ topics. See them all