Overview
An array is a fundamental data structure used in many programming languages to hold a sequence of elements. Each element occupies a position in the sequence identified by a numeric index. Typical element types include integers, strings, or other values of the same type. Arrays provide direct access to elements by index, making them suitable for ordered collections where positional access is important.
Structure and key characteristics
Several properties distinguish arrays from other containers:
- Indexed access: Elements are retrieved by index. In many languages the first index is zero; in others it may start at one. That index is used by the programmer to reference individual entries.
- Homogeneous elements: Elements generally share a common type in statically typed languages, which simplifies storage and access.
- Contiguous memory layout: Elements are typically stored in adjacent memory locations, enabling constant-time arithmetic to compute an element's address and improving cache performance.
- Dimensions: Arrays can be one-dimensional (a list), two-dimensional (a matrix) or multi-dimensional, depending on the language and use case.
Creation, size and variants
When an array is created in many lower-level languages, its capacity (the number of slots) is specified up front as the size. That fixed-size array cannot be extended without allocating a new array and copying elements. Higher-level languages or libraries offer variants that abstract this restriction: dynamic or resizable arrays (often called vectors, ArrayLists, or dynamic arrays) grow automatically by allocating larger storage and migrating elements when needed. Some languages also provide specialized array-like types that allow mixed element types or implicit resizing.
Common uses and examples
Arrays are used wherever ordered collections and fast positional access are required. Common examples include:
- Storing sequences of values such as numbers or text.
- Representing matrices and grids in scientific and image processing.
- Implementing other data structures—stacks, queues, hash tables and heaps—where arrays provide the underlying storage.
- Buffers for input/output and low-level memory manipulation in systems programming.
Performance, limitations and alternatives
Arrays give O(1) time for accessing an element by index (constant-time address computation), and they exhibit good locality of reference because elements are adjacent in memory. However, fixed-size arrays cannot change length without reallocation and copying, and accessing an index outside the valid range typically causes an error. When frequent insertions or deletions in the middle are required, linked lists, trees, or other containers may be more appropriate. Dynamic arrays mitigate resizing costs by growing geometrically, which yields amortized constant-time append operations.
History and notable distinctions
Arrays are among the oldest and most commonly used data structures in computer science. Their simplicity and efficiency for random access make them central to many algorithms and programming idioms. A key distinction to remember is that an "array" in a statically typed language is often a fixed-size, homogeneous block of storage, whereas higher-level languages may present array-like types with different semantics, such as heterogeneous collections or automatic resizing.
For further reading on how specific languages implement arrays and their idioms, consult language documentation or introductory materials on programming languages and data structures.