Skip to content

Arrays

In this chapter:

  • The characteristics of an array
  • How to declare an array and initialise its values
  • Use an index number of access each element of the array and either set or change its value
  • Loop through each element of the array using either a for loop or a foreach loop
  • Sort the elements of an array into ascending or descending order
  • Describe the characteristics of a 2D array, declare and initialise the values
  • Use the row index and column index to access an element in a 2D array
  • Traverse a 2D array using nested for loops and the `GetLength()`` method

So far we have worked with simple data types, such as integers, reals and characters. As programs become more complex, it becomes essential to store larger amounts of data and it is impractical to use simple types to do so. For example, you might need to store and process the marks for a class of \(20\) students over a series of \(5\) assignments. It is not practical to use \(100\) separate integer variables to do this. What would you call them? How would you carry out the same operation on each?

For such a situation we need to use an array. An array is a collection of variables, known as elements, where each element is the same data type.

Overview of arrays
Arrays

The elements are numbered, starting with \(0\), the number acts as an index to access each element. The first element is known as the lower bound of the array, the last as the upper bound. The lower bound will always by \(0\) in C#. This is important to remember when iterating through an array, as the upper bound will always be one less than its length (the number of items in the array). Once it has been initialised the number of elements in the array is fixed.

An array can be multi-dimensional but most commonly one or two dimensions only, anything beyond this gets difficult to manage.

In C# an array is an object, which means it needs to be declared using different syntax.

From the syllabus

AQA: Single- and multi-dimensional arrays (3.2.1.2/4.2.1.2): Use arrays (or equivalent) in the design of solutions to simple problems

  • An array is a data structure that can store multiple values in a single variable.