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 reference or revision guide. 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.

 

Skill
Description
VB.NET
Python
C#
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.

names = ["Percy", "James", "Gordon"]
string[] names = {"Percy", "James", "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.

ages = [14, 15, 14]
int[] ages = {14, 15, 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

names = ["Percy", "James", "Gordon"] print(names[0])
string[] names = {"Percy", "James", "Gordon"}; Console.WriteLine(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

fruit = [["Plumb", "Purple"], ["Apple", "Red"], ["Banana", "Yellow"]] print(fruit[0][1])
string[,] fruit = { {"Plumb", "Purple"}, {"Apple", "Red"}, {"Banana", "Yellow"} }; Console.WriteLine(fruit[0,1]);
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.

colours = [] colours.append("Red") colours.append("Green") colours.append("Blue") print(colours[0])
// using System.Collections.Generic; List<string> colours = new List<string>(); colours.Add("Red"); colours.Add("Green"); colours.Add("Blue"); Console.WriteLine(colours[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.

numbers = [] numbers.append(1) numbers.append(2) numbers.append(3) print(sum(numbers))
// using System.Collections.Generic; // using System.Linq; List<int> numbers = new List<int>(); numbers.Add(1); numbers.Add(2); numbers.Add(3); // Sum requires System.Linq Console.WriteLine(numbers.Sum());
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.

names = ["Alice", "Bob", "Charlie"] names.pop(0)
// using System.Collections.Generic; List<string> names = new List<string> { "Alice", "Bob", "Charlie" }; names.RemoveAt(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

# Create a string storing comma separated values colours = "red,green,blue" # Split the values into an array of strings parts = colours.split(",") # Get the first value in the array print(parts[0])
// Create a string storing comma separated values string colours = "red,green,blue"; // Split the values into an array of strings string[] parts = colours.Split(","); // Get the first value in the array Console.WriteLine(parts[0]);

Loading...