On this page:
6.1.1 One-dimensional Arrays

6.1 Arrays

One of the most important data structures that we will discuss is the array. An array is a collection of elements, arranged in a particular way. There are several types of arrays; the most common are one-dimensional arrays (also called vectors) and bi-dimensional arrays (also called matrixes). Bi-dimensional arrays usually arrange elements in rows and columns, as shown below:

\[\begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n}\\ a_{21} & a_{22} & \cdots & a_{2n}\\ \vdots & \vdots & \ddots & \vdots\\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix}\]

As can be seen in the previous description, in order to specify a particular element of an array, one has to provide a sequence of indexes. For example, \(a_{12}\) is the element located in the first row, second column, which, in Julia, is written as a[1,2]. The number of indexes required depends on the dimension of the array; in the above example, the array is bi-dimensional and, thus, it requires two indexes.

Julia provides a specific syntax to create arrays and also numerous operations to deal with them. In the next section, we will focus on one particular kind of array: the one-dimensional array, also called vector.

6.1.1 One-dimensional Arrays

Although arrays can have as many dimensions as we want, it is common to use arrays with just one dimension. A one-dimensional array is also called a vector and, in Julia, it is considered a column vector (since elements are arranged in a single column):

\[\begin{bmatrix} a_{1}\\ a_{2}\\ \vdots\\ a_{n} \end{bmatrix}\]

In Julia, we can build an array by enclosing the elements, separated by a comma, in square brackets:

> [7, 2, 4, 5]

4-element Vector{Int64}:

 7

 2

 4

 5

Given that this type of array has only one dimension, accessing one of its elements requires only one index. In Julia, the index is provided right after an array, enclosed in square brackets. The last element can be accessed using the special index end. Here is an interaction with Julia illustrating the use of indexes in one-dimensional arrays:

> numbers = [7, 2, 4, 5]

4-element Vector{Int64}:

 7

 2

 4

 5

> numbers[2]

2

> numbers[4]

5

> numbers[begin]

7

> numbers[begin+1]

2

> numbers[end]

5

> numbers[end-1]

4

Another important operation with arrays is the concatenation. It allows us to join the elements of two (or more) arrays. This can be done with the ellipsis operator ... (also called the splat operator):

> more_numbers = [3, 9]

2-element Vector{Int64}:

 3

 9

> [numbers..., more_numbers...]

6-element Vector{Int64}:

 7

 2

 4

 5

 3

 9

> [7, more_numbers...]

3-element Vector{Int64}:

 7

 3

 9

Note that the ellipsis operator incorporates the elements of an existing array into the new array being created. Without the ellipsis operator, the entire array becomes itself an element of the new array, as is visible in the following interaction:

> [7, more_numbers]

2-element Vector{Any}:

 7

  [3, 9]

Julia supports arrays of arrays to an indefinite number of levels, for example: [1, 2, [3, [4], 5], [[[6], 7]], 8].

To “decompose” arrays, Julia provides the concept of slice. An array slice is a section of an array (called subarray), specified by a range of indexes, i.e., a starting and an ending index. Consider the following examples:

> numbers[1:3]

3-element Vector{Int64}:

 7

 2

 4

> numbers[2:end]

3-element Vector{Int64}:

 2

 4

 5

> numbers[2:end-1]

2-element Vector{Int64}:

 2

 4

> numbers[2:2]

1-element Vector{Int64}:

 2

> numbers[2:1]

Int64[]

In the last example, the result is an empty array. This array can also be created by simply writing []. For instance, we have:

> numbers[2:1] == []

true