An array is an ordered linear data structure consisting of a collection of elements (values, variables, or references), each identified by one or more indexes. When asking about specific variants of arrays, use these related tags instead: [vector], [arraylist], [matrix]. When using this tag, in a question that is specific to a programming language, tag the question with the programming language being used.
An array is an ordered linear data structure consisting of a collection of elements (values or variables), each identified by at least one index, stored in contiguous memory locations.
An array is typically stored so that the position of each element can be computed from its index tuple by a mathematical formula.
In some languages (C, Java, etc.) the length of the array must be set beforehand. In other languages (Ruby, Python, LISP, Haxe, etc.) the length of the array grows dynamically as elements are added.
When tagging a question with this tag, also tag the question with the programming language being used.
Array in specific languages
- C# arrays are actually objects and not just addressable regions of contiguous memory as in C and C++. Array is the abstract base type of all array types. You can use the properties and other class members of the Array base type.
- C arrays act to store related data under a single variable name with an index, also known as a subscript. They are stored in row-major order, which means the last subscript varies fastest. It is easiest to think of an array as simply a list or ordered grouping for variables of the same type. As such, arrays often help a programmer organize collections of data efficiently and intuitively.
- C++ inherits raw arrays from C and adds its own array-class
std::arrayfor compile-time array sizes,
std::vectorfor runtime dynamic sized arrays. It also has smart pointer implementations like
- Objective C inherits raw arrays from C and adds its
NSMutableArrayfor dynamic arrays.
- Ruby's normal array class is called
- In Python,
the normal array datatype is called a
list, while the
arraytype is used for homogeneous arrays.
- In NumPy there is a powerful N-dimensional array with many capabilities.
- PHP arrays are implemented as ordered maps which may contain a mix of numeric or string keys.
- In Haxe,
an Array has one type parameter which corresponds to the type
of collection of elements.
Arrays can be created using their constructor
[1, 2, 3], but also using Array comprehension:
[for (i in 0...10) if (i % 2 == 0) i]. For storage of fixed size the abstract type
haxe.ds.Vectorcan be used, which may be faster than Array on some targets and is never slower.
- In Scala,
the normal array class is called
Array. To get an element from an array you use parentheses (most languages use square brackets).
- In Java, an array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
- In Perl,
array variables are denoted with the
@prefix and arrays are declared with parentheses. Replacing the prefix with
$#returns the last index.
- In Rust, arrays are groups of data of the same type that are contiguous in memory, so they can be used when communicating with C. The length of an array is fixed.
- In Swift, an array that includes the specified values, automatically inferring the array’s Element type. Swift makes it easy to create arrays in your code using an array literal: simply surround a comma-separated list of values with square brackets.
- In Pascal, arrays declarations specify the index range rather than the number of elements; so
parray: array [1..25] of real;declares a one-based array of 25 real numbers (valid elements are
parray: array [0..24] of real;declares a zero-based ('C-style') array of the same size (the first element is
parray). Pascal array index ranges can include negative numbers!
Elements of an array are usually specified with a 0-first index,
myarray would represent the first element of
n is the length of the array minus 1)
would represent the last element in the array.
However, some languages such as old Fortran and Lua use 1 as the initial index.
Some languages (C++, Java, C#) have "basic arrays" and collections.
Basic arrays are supported by the compiler directly,
have a fixed size,
and only provide element access by index.
are system library classes implemented on the top of these basic arrays
and have a wide range of various methods.
In such cases,
the tag arrays should be used to name the simple arrays.
Arrays can be statically allocated or dynamically allocated. The way in which you access the array and its type varies based on how it is declared and allocated.
Arrays can contain multiple indices.
For example, an array with one index (e.g.
is known as a one-dimensional array.
If it has two indices (e.g.
array) it is considered two dimensional
and maybe visualized as a grid.
in other words,
arrays with multiple indices are called matrices,
or a matrix is singular.
- Array Data Type (Wikipedia)
- Array Data Structure (Wikipedia)
- Introduction to Java arrays from Princeton's "Introduction to Programming"
- The NumPy array object
Where talking about specific variants of arrays, use these related tags instead: