Categories: All Variables and Constants  String manipulation  Builtin functions  Input and output  Arrays  Conditional logic  Repetition / Iteration  Functions / Procedures 

Show:

This resource is designed as a quick revision guide for use by students during a NEA providing that internet access is allowed. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.

Arrays

Strings, reals, booleans and integers are all primitive data types: they only store one value at a time.

An array lets you store more than one value of the same primitive data type in the same variable.

A variable can store one primitive data type or it can contain an array.

An array stores data in the order that you specify in your code. The position of a value inside an array is called the index.

Top tips:

  • Arrays can only store one data type (e.g. all strings or all integers)
    Python doesn't have arrays but it does have lists, which are more powerful and let you store a mixture of different data types.
  • Both VB.NET and Python start counting the index of an array from 0.
    This means what we think of as being the 1st item in an array has an index of 0.

 

Create array of strings
   

An array let your store more than one piece of data into the same variable as an ordered list.

This example stores three strings into the array called names.

SET names TO ["Percy", "James", "Gordon"]
Dim names As String() names = {"Percy", "James", "Gordon"}
names = ["Percy", "James", "Gordon"]
array names[3] names[0] = "Percy" names[1] = "James" names[2] = "Gordon"
names[3] set names[0] = "Percy" set names[1] = "James" set names[2] = "Gordon"
Create array of numbers
   

An array lets you store more than one piece of data into the same variable as an ordered list.

This example stores three numbers into the array called ages.

SET ages to [14, 15, 14]
Dim ages As Integer() ages = {14, 15, 14}
ages = [14, 15, 14]
array ages[3] ages[0] = 14 ages[1] = 15 ages[2] = 14
ages[3] set ages[0] = 14 set ages[1] = 15 set ages[2] = 14
Access data in an array
   

An array lets you store more than one piece of data into the same variable as an ordered list.

Data in an array can be accessed using an index. This is a number which defines the position in the list, which starts counting from 0

This example displays the first string in an array called names

SET names TO ["Percy", "James", "Gordon"] SEND names[0] TO DISPLAY
Dim names As String() names = {"Percy", "James", "Gordon"} MsgBox(names(0))
names = ["Percy", "James", "Gordon"] print(names[0])
array names[3] names[0] = "Percy" names[1] = "James" names[2] = "Gordon" print(names[0])
names[3] set names[0] = "Percy" set names[1] = "James" set names[2] = "Gordon" output names[0]
Create a 2D array
   

If an array is like a list of data, a 2d array is like a grid of data where the data is stored in one variable but organised into rows and columns like a spreadsheet.

This example stores details of 3 different fruit for a supermarket.

It will then display the colour of the first fruit.

The first index will select the row then the second index selects the column

SET fruit TO [["Plum", "Purple"], ["Apple", "Red"], ["Banana", "Yellow"]] SEND fruit[0,1] TO DISPLAY
Dim fruit As String(,) fruit = {{"Plum", "Purple"}, {"Apple", "Red"}, {"Banana", "Yellow"}} MsgBox(fruit(0,1))
fruit = [["Plumb", "Purple"], ["Apple", "Red"], ["Banana", "Yellow"]] print(fruit[0][1])
array fruit[3][2] fruit[0][0] = "Plumb" fruit[0][1] = "Purple" fruit[1][0] = "Apple" fruit[1][1] = "Red" fruit[2][0] = "Banana" fruit[2][1] = "Yellow" print(fruit[0][1])
{ not specified in eduqas pseudocode reference }
Create a list of strings
   

A list is similar to an array: both allow more than one piece of data to be stored in the same variable.

It's not possible to add or remove data to an array after it's been set for the first time but it is possible to add or remove data to a list.

This example creates a list called colours and adds three strings into it before displaying the first item in the list.

SET colours TO ["Red", "Green", "Blue"] SEND colours[0] TO DISPLAY
Dim colours As New List(Of String) colours.Add("Red") colours.Add("Green") colours.Add("Blue") MsgBox(colours(0))
colours = [] colours.append("Red") colours.append("Green") colours.append("Blue") print(colours[0])
array colours[3] colours[0] = "Red" colours[1] = "Green" colours[2] = "Blue" print(colours[0])
colours[3] set colours[0] = "Red" set colours[1] = "Green" set colours[2] = "Blue" output colourd[0]
Create a list of integers
   

list is similar to an array: both allow more than one piece of data to be stored in the same variable.

It's not possible to add or remove data to an array after it's been set for the first time but it is possible to add or remove data to a list.

This example creates a list called numbers and adds three integer numbers into it before displaying the sum of all the numbers added together.

SET numbers to [1, 2, 3] SEND SUM(numbers) TO DISPLAY
Dim numbers As New List(Of Integer) numbers.AddRange({1, 2, 3}) MsgBox(numbers.Sum()) ' You could also do: Dim numbers As New List(Of Integer) numbers.Add(1) numbers.Add(2) numbers.Add(3) MsgBox(numbers(0) + numbers(1) + numbers(2))
numbers = [] numbers.append(1) numbers.append(2) numbers.append(3) print(sum(numbers))
array numbers[3] numbers[0] = 1 numbers[1] = 2 numbers[2] = 3 print(numbers[0] + numbers[1] + numbers[2])
numbers[3] set numbers[0] = 1 set numbers[1] = 2 set numbers[2] = 3 output numbers[0] + numbers[1] + numbers[2]
Remove data from a list
   

list is similar to an array: both allow more than one piece of data to be stored in the same variable.

It's not possible to add or remove data to an array after it's been set for the first time but it is possible to add or remove data to a list.

This example creates a list of strings called names. It adds 3 names to the list then removes the first one.

SET names TO ["Alice", "Bob", "Charlie"] DELETE names[0]
Dim names as New List(Of String) names.AddRange({"Alice", "Bob", "Charlie"}) names.RemoveAt(0)
names = ["Alice", "Bob", "Charlie"] names.pop(0)
array names[3] names[0] = "Alice" names[1] = "Bob" names[2] = "Charlie" # spec doesn't say, so I'm just guessing here... remove names[0]
names[3] set names[0] = "Alice" set names[1] = "Bob" set names[2] = "Charlie" # spec doesn't say, so I'm just guessing here... remove names[0]
Splitting a string into an array of strings
    

It can be really useful to split one string into lots of smaller parts whenever you see a particular character.

This example will split the string "red,green,blue" into an array of smaller strings: "red", "green" and "blue"

It will then display the first colour: red

STRING colours SET colours TO "red,green,blue" parts = SPLIT(colours, ",") SEND parts[0] TO DISPLAY
Dim colours as String = "red,green,blue" Dim parts() as String = colours.Split(",") MsgBox(parts(0))
colours = "red,green,blue" parts = colours.split(",") print(parts[0])
colours = "red,green,blue" array parts = colours.split(",") print(parts[0])
colours is string set colours = "red,green,blue" parts[] = colours.split(",") output parts[0]

Loading...