Overview

An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key.[1]

Depending on the language, array types may overlap (or be identified with) other data types that describe aggregates of values, such as lists and strings. Array types are often implemented by array data structures, but sometimes by other means, such as hash tables, linked lists, or search trees.[2] In Python, the built-in array data structure is a list.

Discussion

An array is a sequenced collection of elements of the same data type with a single identifier name. Python lists are similar to arrays in other languages but are not restricted to a single data type. The term ‘array’ as used in this chapter will generally also apply to Python lists unless otherwise noted.

Arrays can have multiple axes (more than one axis). Each axis is a dimension. Thus a single-dimension array is also known as a list. A two-dimension array is commonly known as a table (a spreadsheet like Excel is a two dimension array). In real life, there are occasions to have data organized into multiple-dimension arrays. Consider a theater ticket with section, row, and seat (three dimensions). Most single-dimension arrays are visualized vertically.

Most programmers are familiar with a special type of array called a string. Strings are basically a single dimension array of characters. Unlike other single dimension arrays, we usually envision a string as a horizontal stream of characters and not vertically as a list.

We refer to the individual values as members (or elements) of the array. Programming languages implement the details of arrays differently. Because there is only one identifier name assigned to the array, we have operators that allow us to reference or access the individual members of an array. The operator commonly associated with referencing array members is the index operator. It is important to learn how to define an array and initialize its members.

Defining an Array

Language Example
C++ int ages[] = {49, 48, 26, 19, 16};
C# int[] ages = {49, 48, 26, 19, 16};
Java int[] ages = {49, 48, 26, 19, 16};
JavaScript var ages = [49, 48, 26, 19, 16];
Python ages = [49, 48, 26, 19, 16]
Swift var ages:[Int] = [49, 48, 26, 19, 16]

This is the defining of storage space. The square brackets [] are used here to create the array with five integer members and the identifier name of ages. The assignment with braces (that is a block) establishes the initial values assigned to the members of the array. Note the use of the sequence or comma operator. We could have also done something similar to:

Language Example Initial Values
C++ int ages[5]; undefined
C# int[] ages = new int[5];
0
Java int[] ages = new int[5];
0
JavaScript var ages = Array(5);
undefined
Python ages = [None] * 5 None

This would have declared the storage space of five integers with the identifier name of ages but their initial values would have been unknown values or initialized as indicated, depending on the programming language. We could assign values later in our program by doing the following (leaving off the semicolons in Python):

ages[0] = 49;
ages[1] = 48;
ages[2] = 26;
ages[3] = 19;
ages[4] = 16;

Note: The members of the array go from 0 to 4; NOT 1 to 5. This is explained in more detail on the next page.

Key Terms

dimension
An axis of an array.
list
A single dimension array.
table
A two-dimension array.

References


License

Icon for the Creative Commons Attribution-ShareAlike 4.0 International License

Programming Fundamentals Copyright © 2018 by Authors and Contributors is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book