Working with Arrays


An array is a special type of variableit's a variable with multiple dimensions. Think of an ordinary variable as a single mail slot. You can retrieve or change the contents of the mail slot by referencing the variable. An array is like having an entire row of mail slots (called elements). You can retrieve and set the contents of any of the individual mail slots at any time by referencing the single array variable. You do this by using an index that points to the appropriate slot.

Declaring Arrays

Arrays are declared in much the same way as ordinary variables, with one notable exception. Consider the following statements:

string[] strMyArray; strMyArray = new string[10];


The first statement declares strMyArray as an array, and the second statement defines the array as having 10 string elements.

The number in brackets determines how many "mail slots" the array variable will contain, and it can be a literal value, a constant, or the value of another variable.

By the Way

It's possible to create arrays that can be resized at runtime. However, this is beyond the scope of this book.


Referencing Array Variables

To place a value in an array index, you specify the index number when referencing the variable. Most computer operations consider 0 to be the first value in a series, not 1, as you might expect. This is how array indexing behaves. For example, for an array dimensioned with 10 elementsdeclared using [10]you would reference the elements sequentially using the indexes 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

By the Way

Notice that the upper index is one less than the number specified when the array was declared. Because 0 is a valid element, you end up with one more than the number you used to declare the array. This can be confusing. To simplify your development, you might just consider ignoring element 0 and using elements 1 through the declared upper value.


To place a value in the first element of the array variable, you would use 0 as the index, like this:

strMyArray[0] = "This value goes in the first element";


To reference the second element, you could use a statement like this:

strMyArray[1] = strMyArray[0];


By the Way

The data type specified for the array variable is used for all the elements in the array. You can use the Object type to hold any type of data in any element, but doing so isn't recommended for all the reasons discussed earlier.


Creating Multidimensional Arrays

Array variables require only one declaration, yet they can store numerous pieces of data; this makes them perfect for storing sets of related information. The array example shown previously is a single-dimension array. Arrays can be much more complex than this example and can have multiple dimensions of data. For example, a single array variable could be defined to store personal information for different people. Multidimensional arrays are declared with multiple parameters such as the following:

int[,] intMeasurements; intMeasurements = new int[3,2];


These statements create a two-dimensional array. The first dimension (defined as having three elements: 0, 1, 2) serves as an index to the second dimension (defined as having two elements). Suppose that you wanted to store the height and weight of three people in this array. You reference the array as you would a single-dimension array, but you include the extra parameter index. The two indexes together specify an element, much as coordinates in Battleship relate to specific spots on the game board. Figure 11.2 illustrates how the elements are related.

Figure 11.2. Two-dimensional arrays are like a wall of mail slots.


Elements are grouped according to the first index specified; think of the first set of indexes as being a single-dimension array. For example, to store the height and weight of a person in the array's first dimension (remember, arrays are zero-based), you could use code such as the following:

intMeasurements[0,0] = FirstPersonsHeight; intMeasurements[0,1] = FirstPersonsWeight;


I find it helpful to create constants for the array elements, which makes array references much easier to understand. Consider this:

const int c_Height = 0; const int c_Weight = 1; intMeasurements[0,c_Height] = FirstPersonsHeight; intMeasurements[0,c_Weight] = FirstPersonsWeight;


You could then store the height and weight of the second and third person like this:

intMeasurements[1,c_Height] = SecondPersonsHeight; intMeasurements[1,c_Weight] = SecondPersonsWeight; intMeasurements[2,c_Height] = ThirdPersonsHeight; intMeasurements[2,c_Width] = ThirdPersonsWeight;


In this array, I've used the first dimension to differentiate people. I've used the second dimension to store a height and weight for each element in the first dimension. Because I've consistently stored heights in the first slot of the array's second dimension and weights in the second slot of the array's second dimension, it becomes easy to work with these pieces of data. For example, you can retrieve the height and weight of a single person as long as you know the first dimension index used to store the data. You could print out the total weight of all three people using the following code:

Console.WriteLine(intMeasurements[0,c_Weight] + intMeasurements[1,c_Weight] +                 intMeasurements[2,c_Weight]);


When working with arrays, keep the following points in mind:

  • The first element in any dimension of an array has an index of 0.

  • Dimension an array to hold only as much data as you intend to put into it.

  • Dimension an array with a data type appropriate to the values to be placed in the array's elements.

Arrays are a great way to store and work with related sets of data in Visual C# code. Arrays can make working with larger sets of data much simpler and more efficient than using other methods. To maximize your effectiveness with arrays, study the for loop discussed in Hour 14, "Looping for Efficiency." Using a for loop, you can quickly iterate (loop sequentially) through all the elements in an array.

By the Way

This section discussed the rectangular type of a Visual C# multidimensional array. Visual C# also supports another type of multidimensional array called jagged. Jagged arrays are an array of one-dimensional arrays, each of which can be of different lengths. However, teaching jagged arrays is beyond the scope of this book.





Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net