Arrays



Summary

An array stores multiple data values of the same data type in one and the same variable. This can be handy for loops because multiple variable can't be used for a loop but an arrays can.

In this tutorial the usage of the array will be explained with code examples.

Coding

Let us implement the array in the following situation:

We are searching for the number 16 in a list of integer variables.

Without the array we would have a lot of variables.

class Program
{
static void Main(string[] args)
{
int number1 = 4;
int number2 = 8;
int number3 = 15;
int number4 = 16;
int number5 = 23;

if (number1 == 16)
{
}
else if (number2 == 16)
{
}
else if (number3 == 16)
{
}
}
}

The syntax for the Array looks like this:    <DataType>[] <Variablenname> = new <DataType>[<Length of the Array>];

The brackets after the data type tells the program that it is an array.

To retrieve a value from the array:  <Variablenname>[<Index>]

The index starts with zero. That means that an array of the length 2 has the index 0 and 1.

Let's look at an example on how to create an array with values:

int[] numbers = new int[5];

numbers[0] = 4;
numbers[1] = 8;
numbers[2] = 15;
numbers[3] = 16;
numbers[4] = 23;

Console.WriteLine(numbers[1]);
Console.WriteLine(numbers.Length);
Console.ReadLine();

As you can see the initialization has roughly the same amount of lines.

But now the array can be run through with an array by using the index of the array.

A possibility to write the initialization shorter exists.

That way we don't have to specify the length of the array because the program counts the number of values and uses that as the length for the array.

int[] numbers = new int[] { 4, 8, 15, 16, 23, 42 };

string[] names = new string[] { "Eddie", "Alex", "Michael", "David Lee" };

This works for every data type. It is also possible to create an array of an array.

By using an index larger that the length of the array an exception will be thrown.

Let's try printing each value from the array into the console by using loops.

for (int i = 0; i < names.Length; i++)
{
Console.WriteLine(names[i]);
}

// Alternative zur For-Schleife
foreach (string name in names)
{
Console.WriteLine(name);
}

Console.ReadLine();

The array has a number of methods like a sort method or a method to reverse the array.

The String data type has a method called "ToCharArray()" because a string is literally a list of chars.

Like this we can print the sentence backwards into the console:

string zig = "You can get what you want out of life " +
" if you help enough other people get what they want.";

char[] charArray = zig.ToCharArray();
Array.Reverse(charArray);

foreach (char zigChar in charArray)
{
Console.Write(zigChar);
}
Console.ReadLine();

Complete program code:

class Program
{
static void Main(string[] args)
{
/*int number1 = 4;
int number2 = 8;
int number3 = 15;
int number4 = 16;
int number5 = 23;

if (number1 == 16)
{
}
else if (number2 == 16)
{
}
else if (number3 == 16)
{
}*/

/*int[] numbers = new int[5];

numbers[0] = 4;
numbers[1] = 8;
numbers[2] = 15;
numbers[3] = 16;
numbers[4] = 23;

Console.WriteLine(numbers[1]);
Console.WriteLine(numbers.Length);
Console.ReadLine();*/

int[] numbers = new int[] { 4, 8, 15, 16, 23, 42 };

string[] names = new string[] { "Eddie", "Alex", "Michael", "David Lee" };

for (int i = 0; i < names.Length; i++)
{
Console.WriteLine(names[i]);
}

foreach (string name in names)
{
Console.WriteLine(name);
}

Console.ReadLine();

string zig = "You can get what you want out of life " +
" if you help enough other people get what they want.";

char[] charArray = zig.ToCharArray();
Array.Reverse(charArray);

foreach (char zigChar in charArray)
{
Console.Write(zigChar);
}
Console.ReadLine();
}
}