Learn Programming

Learn D Programming Lesson 7 - Arrays

What is an array?

An array is a data structure that can store multiple values of the same data type in order using only one name. Each item in an array has an index and a value. An index is the position of the value in an array. Indexes do not start at 1 like you would think but rather at 0. Here is a visualization of what an array looks like.

IndexValue
056
123
278

You can see that the above array has 3 items in it. The index of the first item is 0 and the index of the third item is 2. Each index has a value which are all just random numbers. The value also has a data type which in this example is int but the data type can be anything else like string or char.

Static arrays

The most basic type of array is called a static array. When you declare one you have to specify how many items you want the array to contain. Here is an example of how to declare an array of integers that contains 3 items.

int[3] MyArray;

To set the values of an array you can either initialize the array or you can set the values individually. You initialize an array by setting it to some values in square brackets separated by commas. Here is an example of how to initialize an array.

int[3] MyArray = [56,23,78];

You set the values of an array individually by putting square brackets after the name of the array with the index inside them. Remember that the index starts at 0. Here is an example of how to set the values of an array.

int[3] MyArray;
MyArray[0] = 56;
MyArray[1] = 23;
MyArray[2] = 78;

If you need to get the value of an item in an array you also use square brackets just like when you set the value. Here is an example that prints out the 3 values in an array.

int[3] MyArray;
MyArray[0] = 56;
MyArray[1] = 23;
MyArray[2] = 78;
writefln(MyArray[0]);
writefln(MyArray[1]);
writefln(MyArray[2]);

Dynamic arrays

A dynamic array is an array whose length you can change at any time. Dynamic arrays are useful for when you don't know how many items you will be adding to an array. A dynamic array is declared in the same way as a static array but without the number of items in brackets. After you declare a dynamic array you have to set its initial length(number of items) before you will be able to use it. You set the length again when you want to change the number of items in it. Here is an example that shows how to create a dynamic array containing 3 items which is resized to contain 5 items.

int[] MyArray;
MyArray.length = 3;
MyArray[0] = 56;
MyArray[1] = 23;
MyArray[2] = 78;
MyArray.length = 5;
MyArray[3] = 12;
MyArray[4] = 45;

Array length

You can get the number of items in an array using the .length property. Here is an example that prints out the length of an array.

int[3] MyArray;
writefln(MyArray.length);

Associative arrays

An associative array is a type of array that uses keys instead of indexes to reference its items. A key can be any data type like a string or even an integer. To use an item in an associative array you put the key inside the square brackets instead of an index number. Here is an example of how to declare an associative array which uses a string as the key type.

int[string] MyArray;
MyArray["abc"] = 56;
MyArray["xyz"] = 23;
writefln(MyArray["abc"]);
writefln(MyArray["xyz"]);

The array in the above example contains integers. It uses a string as the key type. You can see that the key can be any random string and replaces the index number which is used by normal arrays.

If you want to remove an item from an associative array then you must use the .remove command on it with the key. Here is an example of how to remove a key.

int[string] MyArray;
MyArray["abc"] = 56;
MyArray.remove("abc");

You can get a list of keys in an associative array by using the .keys property. It will return a dynamic array containing the keys. Here is an example that gets the keys of an associative array and then prints them all out.

int[string] MyArray;
MyArray["abc"] = 56;
MyArray["xyz"] = 23;
string[] keys = MyArray.keys;
for (int i = 0; i < keys.length; i++)
   writefln(keys[i]);

Multi-dimensional arrays

A multi-dimensional array is an array that uses more than one index and is an array of arrays. The most commonly used type of multi-dimensional array is the 2D array. The best way of understanding what a 2D array looks like is to look at the following visualization.

 012
0123
1456
2789

As you can see it looks like a square because it has both a length and a width. The length is often called x and the width is often called y. The numbers from 1 to 9 are the values in the 2D array.

You declare a 2D array by using 2 sets of square brackets after the data type. When you access the values in the array you have to use 2 indexes. Here is how you would create a 2D array and set all of its values.

int[3][3] MyArray;
MyArray[0][0] = 1;
MyArray[0][1] = 2;
MyArray[0][2] = 3;
MyArray[1][0] = 4;
MyArray[1][1] = 5;
MyArray[1][2] = 6;
MyArray[2][0] = 7;
MyArray[2][1] = 8;
MyArray[2][2] = 9;

Sorting arrays

You will often need to sort arrays and there are 2 ways that you can do it. The first is to use the .sort command of an array. Here is an example of how to use the .sort command to sort an array.

int[5] MyArray;
MyArray[0] = 56;
MyArray[1] = 23;
MyArray[2] = 78;
MyArray[3] = 12;
MyArray[4] = 45;
MyArray.sort;
for(int i = 0; i < MyArray.length; i++)
   writefln(MyArray[i]);

It is important that you know how to do a manual sort because you won't always be able to rely on there being a .sort command in all situations. There are many different ways to sort but the one we are going to use is the selection sort. The selection sort loops through an array and finds the smallest item and replaces the current one with it. Here is the code for a selection sort that sorts an array.

int[5] MyArray;
MyArray[0] = 56;
MyArray[1] = 23;
MyArray[2] = 78;
MyArray[3] = 12;
MyArray[4] = 45;

// Selection sort
for (int i = 0; i < MyArray.length - 1; i++)
{
   int MinIndex = i;
   for (int j = i + 1; j < MyArray.length; j++)
   {
      if (MyArray[j] < MyArray[MinIndex])
         MinIndex = j;
   }
   int temp = MyArray[i];
   MyArray[i] = MyArray[MinIndex];
   MyArray[MinIndex] = temp;
}

for(int i = 0; i < MyArray.length; i++)
   writefln(MyArray[i]);

Foreach loops

The foreach loop is a type of loop that is similar to a for loop except it does all the thinking for you because it chooses which positions in the array to loop though. A foreach loop starts with the foreach keyword followed by brackets. Inside the brackets are 2 instructions. The first is where you declare the variable of the same type as the data type of the array items. This variable will contain the value for the item for that iteration of the loop. The second instruction is the name of the array to loop through. Here is an example of how to loop through and print out the values of an array using a foreach loop.

int[3] MyArray;
MyArray[0] = 56;
MyArray[1] = 23;
MyArray[2] = 78;

foreach(int val; MyArray)
   writefln(val);

Practice

Write a program that asks the user to enter 5 numbers and stores them in an array. The program must then sort the items in the array and print them on the screen. You can use either the .sort command or you can use a selection sort to sort the array. For a real challenge you can do some research to find out what a bubble sort is and use it to sort the items in the array in the program.