teach-ict.com logo

THE education site for computer science and ICT

3. Using arrays

On the previous page, you learned how to 'declare' an array, making it available to the program:

               SET PlayerNames TO ["John Smith", "Simon Says", "Jimmy Widget"]

The array will not change the order of the items it holds unless it is told to. The position of an item is often referred to as a number called an index. The first item in the list is at index 0, and the index increases by 1 for each number afterwards.

So PlayerNames[0] is "John Smith", and "Jimmy Widget" is PlayerNames[2].

You can change individual items within an array without affecting the rest by using their index number. The following line of pseudocode would change "Simon Says" to "Michael Windsor", for example:

               SET PlayerNames[1] TO "Michael Windsor"
  

You can use other commands to retrieve the length of the array (the number of items it holds), or search for a particular value within its contents, or sort the contents, or any number of other things. Arrays are a very easy and flexible data structure.

 

An array is a collection of related data. Each piece of data within the array is an element. The position of each element within the array is pointed to be its index. Arrays usually begin at index 0, not index 1.

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: What is a one dimensional array