Programming Fundamentals/Arrays and Lists

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Overview[edit | edit source]

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.

Static arrays have a fixed size. Their length cannot be changed. On the other hand, dynamic array lengths can be altered. According to geeksforgees.org, dynamic arrays are described as a simple dynamic array that can be constructed by allocating an array of fixed-size, typically the number of elements immediately required. The elements of the dynamic array are stored contiguously at the start of the underlying array. Elements can be added at the end of a dynamic array in real time by using reserved memory space until this space is completely consumed.[3][4]

Discussion[edit | edit source]

An array is a data structure consisting of a collection of elements, each identified by an 'array index' or 'key'. Each axis of an array is called a dimension. A 'list' is a single dimension array. A 'table' is a 2 dimensional array. The index notation specifies elements of an array, using brackets [] or {} depending on the programming language, to store the elements into the array. A string is 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.

Individual values of the array are called members or elements. The operators allow us to reference or access the individual members. The operator commonly associated with referencing array members is the index operator.

Defining an Array[edit | edit source]

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 process is known as 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[edit | edit source]

array
is a data structure consisting of a collection of elements (values or variables).
dimension
An axis of an array.
index notation
Specifies elements of an array, using brackets [] or {} depending on the programming language
list
A single dimension array.
table
A two-dimension array.

References[edit | edit source]