鶹Լ

Using arrays

To store a item in an , the that the data will be stored in needs to be referenced.

For example, in the score array score(10), the following would store the score 3000 in the first element:

score[0] = 3000

The following would store the score 2500 in the second element:

score[1] = 2500

Computers start counting at 0, so the first element is [0], the second is [1], etc.

To access the data stored in an element, the element must be referred to by its number. For example:

print(score[0])
total = score[0] + score[1]

Why use arrays?

A holds a single item of data. There may be a situation where lots of variables are needed to hold similar and related data. In this situation, using an array can simplify a program by storing all related data under one name. This means that a program can be written to search through an array of data much more quickly than having to write a new line of code for every variable. This reduces the complexity and length of the program which makes it easier to find and debug errors.

Lists

Lists are similar to arrays that allow data of more than one .

Some languages, such as and allow the use of arrays. Others, such as , only allow lists.