Skip to main content

An array is an ordered data structure consisting of a collection of elements (values or variables), each identified by one (single dimensional array, or vector) or multiple indexes.

Description

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, etc.) the length of the array grows dynamically as elements are added.

  • Elements of an array are generally specified with a 0-first index, for example, myarray[0] would represent the first element of myarray. myarray[l] (where 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 (, , ) 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 , 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.

Array in specific languages

  • Ruby

    In the normal array class is called an Array.

  • Python

    In the normal array datatype is called a list, while the array type is used for homogeneous arrays.

  • PHP

    In arrays are implemented as ordered maps which may contain a mix of numeric or string keys.

  • JavaScript

    In arrays are just objects with a different prototype (with additional functions more useful for array-like structures), with numerical indexes stored as strings (all JavaScript keys are strings).

Tag Guidelines

When tagging a question with this tag, also tag the question with the programming language being used.

References