Coding With Flavio
Lesson Summary:
Arrays
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.
​