Arrays

What Constitutes an Array in Programming?

An array is made of elements, indexes, a length, and usually one element type. Learn what each part means and how it works across common languages.

On this page
  1. The short answer
  2. Elements
  3. Indexes
  4. Length and element type
  5. Multi-dimensional arrays
  6. Examples in two languages
  7. What does not count as an array
  8. Where this leads

The short answer

An array is an ordered collection of elements. Each element has an index, which is its position in the collection. Most arrays also have a length, which is the number of elements, and in typed languages, a single element type shared by every element. Those are the parts that usually make up an array.

Elements

An element is one value stored in the array. It might be a number, a character, a string, or a reference to another object. The array is the whole collection, and each slot inside it holds one element.

Indexes

An index is the position you use to read or write an element. In C, Java, and Python, the first element has index 0, so an array of five items has indexes 0 through 4. Some languages, such as Lua and MATLAB, count from 1 instead. Check the language you are using before you write a loop, because off-by-one errors are common.

Length and element type

The length is how many elements the array holds. Some arrays have a length that is fixed when they are created. C and Java arrays work this way. Other containers, such as Python’s list, can grow and shrink as you add or remove items.

The element type says what kind of value each slot can hold. In a typed language like C or Java, every element of an integer array must be an integer. Python lists are more flexible and can mix types in one list, so they are not typed arrays in the strict sense.

Multi-dimensional arrays

An array can hold other arrays. A two-dimensional array is often described as a grid, with rows and columns. In C, the elements of a two-dimensional array are stored row by row in memory. That is called row-major order. Other languages may use different layouts, so check the documentation for yours.

Examples in two languages

In C, the array has a fixed size and a single element type:

int ages[3] = {21, 34, 27};
// ages[0] is 21

In Python, a list works like a simple array for many tasks, and it can hold mixed types:

ages = [21, 34, 27]
# ages[0] is 21

Both examples use an index to read the value, but the rules behind them differ. The C array is fixed in size and type, while the Python list is more flexible.

What does not count as an array

Not every collection of values is an array. A linked list stores nodes that point to the next node, so elements are not in one indexed block of memory. A set has no index at all, and its elements have no fixed position. Those structures have their own rules and costs.

Where this leads

Once you know what makes up an array, the next question is whether it counts as a data structure. Start with is an array a data structure. The short answer there is yes. Readers who want the type-system side can also ask, is an array a composite data type? That question depends on how the term is defined.

The complete guide to arrays

Is an Array a Data Structure?
Read the guide

Keep exploring

Guides from other topics

All topics →