top of page

Lesson 6: Arrays

A more effective way to handle multiple values.

Lesson Summary:

Arrays

As the name implies, an array is basically just a type consisting of a bunch of something. For example, an array of integers of size 5 is basically 5 integers, side-by-side. Think of it like a box with 5 slots, each of which holds 1 integer. We can define an array that contains 5 specific integers in the following way:

​

  int[] myArray = { 5, 22, 2, 6, 99 };

​

The first part, in redindicates that we are declarting an array of integers. This is like declaring any other type, whether it be integer, string, etc.

In tan we have the name of the array. As with any other variable, this can be anything, but should be informative.

In blue we initialized this array with 5 arbitrary integer values.

​

We can get a value of this array using index notation. That is, we can use square brackets, [], to fetch a value from our array. For example:

​

   int firstValue = myArray[0];

​

will set the variable firstValue to the first element on the array (note we start counting from 0, not 1). That is, based on our example, the value of firstValue will be 5. Note that the values we set in the example above are, in order, 5, 22, 2, 6, and 99. We can expect the following:

​

   int first = myArray[0]; // value will be 5

   int second = myArray[1]; // value will be 22

   int third = myArray[2]; // value will be 2

   int fourth = myArray[3]; // value will be 6

   int fifth = myArray[4]; // value will be 99

​

You can also set the values of an array:

​

  myArray[2] = 33;

​

This will set the value of the 3rd item in the array to be 33 instead of 2.

​

Careful going "out of bounds" of your array. For example, the following 2 lines of code will crash the program, given our starting example:

​

  int crash1 = myArray[-1]; // will crash
  int crash2 = myArray[5]; // will crash

​

The first crashes because the lowest possible index for an array is 0. The second crashes because our array was defined to only have 5 values, and myArray[5] is trying to access a non-existing 6th value. Same applies to the folloiwing:

​

  myArray[-1] = 5; // will crash

  myArray[5] = 8;  // will crash

Empty arrays and dynamic array sizes

Arrays have other ways of being created. Most of the time, we want to create an empty array, especially because we don't usually know which values to fill our array with beforehand. Perhaps the most common way to create an array of size, say, 5, is using the following notation:

​

   int[] myArray = new int[5];

​

Note the new keyword, which we will see being used for many other things in the future. This creates an brand new array with 5 elements, each starting at their default value (which is 0 for ints). This is the equivalent of doing the following:

​

   int[] myArray = { 0, 0, 0, 0, 0 };

​

One advantage of using the notation with "new" is that we can initialize our array dynamically by specifying a variable as the size. For example, we can prompt the user for the size of the array:

​

   string input = Console.ReadLine();

   int arraySize = int.Parse(input);

   int[] myArray = new int[arraySize];

​

This will set the size of our array to some number input by our user. The value of arraySize doesn't necessarily have to be set using Console.ReadLine. For example, one may want to create an array on the fly by using some other information, such as a value read from a file, or a randomly generated number.

Iterating through arrays

One of the most common things to do with an array is to iterate through its values. For example, say you have an array with 10 values. You may want to go through those values, one by one, to process them in some way. You might be trying to add up all integers in an array of integers, or trying to spot a specific value in an array of strings, or maybe you just want to print each value to the console.

​

The most common way to iterate through arrays is using a for loop. The following for loop is typically used to go through an entire array of size 5 (in this example, we simply write out each value to console):

​

   int[] myArray = { 3, 66, 22, 1, 9 };

   for (int i = 0; i < 5; i++)

   {

       Console.WriteLine(myArray[i]);

   }

​

Note that in this example we index the array by "i" to get the value of the array at that point in the loop. This will go thru each value in the array, starting from myArray[0], all the way to myArray[4], writing each to the console.

​

Arrays also have their own built-in methods and properties. One of the most useful ones is Length. It returns the length of the array. That is, how many items the array can carry. For example:

​

  int[] myArray1 = { 3, 66, 22, 1, 9 };

  int myLength1 = myArray1.Length; // The value will be 5

​

  int[] myArray2 = new int[10];

  iny myLength2 = myArray2.Length; // The value will be 10

​

We can use this feature to build better for loops for iterating thru arrays. The following will allow us to change our array without having to update the "5" in the for loop:

​

   int[] myArray = { 3, 66, 22, 1, 9 };

   for (int i = 0; i < myArray.Length; i++)

   {

       Console.WriteLine(myArray[i]);

   }

​

for example, we can easilly add a number to the array now without changing the for loop:

​

   int[] myArray = { 3, 66, 22, 1, 9, 33 };

   for (int i = 0; i < myArray.Length; i++)

   {

       Console.WriteLine(myArray[i]);

   }

​

Lastly, there is a nifty special form of the for loop which can be used for things such as arrays. It's the "foreach" loop. It's name basically describes what it does. Take this example:

​

  int[] myArray = { 3, 66, 22, 1, 9 };

  foreach (int value in myArray)

  {

     Console.WriteLine(value);

  }

​

This does the same as the prior example, but more consicely. Note that "value" is the actual value of each element in the array, not its index. This will iterate thru myArray with "value" taking the values 3, 66, 22, 1 and then 9 respectively.

​

Practice: Fibonacci!

bottom of page