Storing Multiple Values with an Array

You have already learned that a variable stores a single value of a datatype. When writing an application, there will be many times when you want to work with a group of values instead of just one.



A DVD tracking system will need a way to store and view your list of movies. A digital cookbook needs to show all your recipes. Instead of creating separate variables for each one of these values, you can create a single variable to hold them all. There are many ways to store a collection of values. This post is going to focus on arrays.


Arrays store a fixed-size collection of values sharing the same datatype. “Fixed-size” means that elements cannot be added or removed from an array. When you create the array object, it’s element count is “fixed.”


Use square brackets [ ] to declare an array variable.

string[] movieList

This movieList variable will store more than one string value.


Initialize an Array

You provide the number of elements it will hold when you initialize the object with the “new” keyword. This can be done in one of two ways.


You can provide the actual number of elements to be stored.

string[] movieList = new string[5];

When this line is executed, the application creates an array with five elements, entering the default value for the String data type into every element.

To add or view values in an array, you need to use the “index” value for the slot you wish to work with. The C# programming language implements what is called “zero-based numbering.” This means that the collection starts with Zero instead of One.

Meaning the “first” element of a collection has an index of 0.


Another way you can set the size of the array is to provide the values of the element when you initialize the object.


This code will populate the array with your collection of movies.


string[] movieList = new string[]
{“Avengers”, “Goonies”, “Notebook”, “Halloween”,ET};

Updating Array Values

To update the value of an array element, you tell Visual Studio the index of the element and the value you want to store.

 movieList[0] = "Hancock";
 movieList[1] = "Gremlins";
 movieList[2] = "Pretty Woman";
 movieList[3] = "Friday the 13th";
 movieList[4] = "Star Wars";

Remember, an array has a fixed-length. You can change the values of the elements, but you can not add or remove elements.


Accessing Array Values

You can access the values of the array elements by using their index numbers.


 Console.WriteLine(movieList[0] + " is a good movie.");
 Console.WriteLine("As a kid, I really liked " + movieList[4] + ".");
 Console.WriteLine("I refuse to ever watch " + movieList[2] + "!");

Working with Arrays

Just as the string datatype has methods you can utilize to work with the string, arrays have a few ways you can work with them as well.


Length

There will be many times when you need to know how many elements are in the arrays. The Length property is the best way to get that number as an integer value.

Console.WriteLine("I have " + movieList.Length + " movies.");

CopyTo()

Since an array can’t grow in size, there will be times when you need the values of one array entered into a larger array. Manually entering all those values into the new array is not efficient.


The CopyTo method allows you to copy all the elements from one array into another array. You can provide an index value to use as a starting point when copying the elements into the new array.


The following code will copy the five movies from movieList into the last five elements of newMovieList.


 string[] newMovieList = new string[10];
 movieList.CopyTo(newMovieList, 5); 

49 views0 comments

Recent Posts

See All