An array is an ordered data structure consisting of a collection of elements (values or variables), each identified by one or multiple indexes. When asking about specific variants of arrays, use these related tags instead: [vector], [arraylist], [matrix]. When using this tag, in a question which is specific to a programming language - tag the question with the programming language being used.
An array is an ordered 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, that Array has.
- 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 also 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 own array-class
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 can 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 array you don't use square brackets as in most other languages but parentheses.
- 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 same type contiguous in memory, so they can be used when communicating with C. The length of an array is fixed.
Elements of an array are generally specified with a 0-first index, for example,
myarray would represent the first element of
l 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 fixed size and only provide element access by index. Collections, like Java's
ArrayList, 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.
array) is known as a one dimensional array. If it has two indices (e.g.
array) it is considered two dimensional, and can be visualized as a grid. Multidimensional arrays, or, in other words, arrays with multiple indices are called matrices, or matrix in 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: