Arrays


Martin McBride, 2017-02-12
Tags none
Categories none

An array is a type of data structure that holds an ordered list of data values. It is very useful if you need to keep a set of related values together.

Declaring an array

You can declare an array like this:

ARRAY days[7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

This creates an array called days which contains 7 strings, representing the short names of the days of the week.

Accessing individual elements

The values in the array are called elements. You can access an individual element using square brackets. Elements are numbered starting from 0, so:

  • days[0] is the first element, "Sun".
  • days[1] is the second element, "Mon".
  • days[2] is the third element, "Tue".

The following code would print Sun, Tue, Fri:

PRINT(days[0])
PRINT(days[2])
PRINT(days[5])

Modifying elements

You can set the elements in an array using assignment, like this:

days[0] = "First"
days[6] = "Last"

The array would now contain:

["First", "Mon", "Tue", "Wed", "Thu", "Fri", "Last"]

Looping through the elements

You can use a for loop to loop through all the elements in an array, like this:

FOR i = 0 TO 6
    PRINT(days[i])
ENDFOR

Notice that the loop counter i starts at 0 and counts up to one less than the length of array (ie 6).

Why does the index start at 0?

In most languages, array indices start at 0. This seems a bit odd at first, but you get used to it and it often makes things easier.

The basic reason is that the elements of an array are stored contiguously (next to each other) in memory. In the array numbers, if addr is the memory address of the start of the array, then:

  • The first element is at addr + 0.
  • The second element is at addr + 1.
  • The third element is at addr + 2.

index

So the index in the array is also the offset from the start of the array to the required element.

Static arrays

The arrays we are discussing in this topic are static arrays. This simply means that you have to decide how long the array is when you create it. Once you have created an array of a particular length, you cannot change its length. Many languages provide static arrays.

The alternative to a static array is a dynamic data structure usually called a list. Lists are more flexible than arrays, for example you can increase their size after they have been create. The downside is that they can be slightly slower, and use more memory

{{% orange-note %}} A few languages, such as JavaScript, feature dynamic arrays. These behave more like lists in other languages. {{% /orange-note %}}

Python and arrays

If you are learning Python, you will probably have learned about lists rather than arrays. This is because Python programmers tend to prefer lists, and don't use arrays very often.

You might have been told that Python doesn't have arrays, or that arrays in Python are called lists. That isn't quite true.

Python has arrays, which are very similar to arrays in other languages. Python also has lists, which are very similar to lists in other languages. The only difference is that Python programmers prefer the flexibility of lists, and only use arrays if they really need the extra efficiency, for example when dealing with image data.

Inserting a value into an array

What if we wanted to insert a number 6 at position 2 in our numbers arrays. So instead of:

[3, 4, 9, 2, 8]

we would have:

[3, 4, 6, 9, 2, 8]

The main problem is that our existing array only has 5 elements, and we are trying to create an array with 6 elements. What is the solution?

Well we must create a brand new array with 6 elements. Then we need to copy the 5 elements from the old array into the new array - but leaving a gap at the second element. Finally we copy the new value, 6, into the second element.

insert

If you find that you need to do this type of thing in your program, you should probably be using lists instead of arrays. They have this functionality already built in, using optimised code.

Copyright (c) Axlesoft Ltd 2021