Topics / Programming / Arrays

Arrays

An array is a data structure that is capable of storing more than one item of data (value) at a time.

To achieve this, each piece of data in an array is given an index number (a unique storage slot, called an element).

Example:

The array burgers[4] has five slots, or elements, where we can store values (arrays start with element 0).

0 1 2 3 4
Zinger Tower Big Mac Fillet Tower Chicken Sandwich Whopper

To assign or read from an array, we simply refer to the array name and index (position) number, e.g. burgers[2] contains “Fillet Tower”.

When a new value is assigned to an array element, it automatically replaces whatever was stored in that position previously.

Using Multiple Arrays

Using multiple arrays we can create relationships between stored data.  To do this we need to ensure that the array index number is consistent for all related data.

name[4]

0 1 2 3 4
Jenny Diane Claire Caragh Ellie

score[4]

0 1 2 3 4
1000 772 534 1423 2425

Although stored across two separate data structures, in the above example we can learn that Ellie has a high score of 2425, and Claire of 534.

Topics / Programming / Arrays