Lists and Collections |
|
To provide a good management of arrays, especially as compared to C++' arrays, the Microsoft .NET Framework provides a class called Array. For this reason, any array variable you create directly descends from the Array class. This class provides various basic operations used to manipulate either the group of items or individual members of the array.
We had mentioned that, when creating an array, before using, you must request an amount of space you would need for the variable. This is done by first specifying the data type that indicates the amount of memory that each member would occupy. Then you must specify the number of members of the array. We also mentioned that you could increase or decrease the number of members of the array with a simple re-assignment. At anytime, you can find out the number of members of the array using the Length property of the Array class.
You can use the Write() or the WriteLine() function to display the value of a member of an array. As we have learned, you can use each member’s index to display its value. Here is an example: By converting each double value to string, this program could also have been written as follows: #u In the same way, you can display the value of any member of the array anytime, using its index: This would produce: Since an array is a list of items, you can scan the array using a for loop. This allows you to perform a common operation on each member of the array. To do this, you can use a for loop that starts with the first index of the array and continues to the last item, visiting each member. Since an array is zero-based, to scan all members of the array, the count would start at 0 and would end at index-1. The syntax you would use is: for(Counter = 0; Counter < (Index - 1); Increment) Process the array; Since the subtraction operation has a higher precedence than the less than comparison (check Appendix A), you can omit the parentheses on the Index - 1 operation above: for(Counter = 0; Counter < Index - 1; Increment) Process the array; To scan the members of the array and display the value of each on a cout extractor, the above code could have been written:
When you initialize the array as a whole, the advantage is that the declaration is more concise. Since the members of the array are set by their index, even if you declare the array as a whole, you can still access an individual member using its position. Since the dimension of an array is a constant integer, sometimes it is more convenient and professional to declare such a constant. This allows more control over the dimension of the array especially when or if you decide to change the dimension. You can easily locate the constant variable that represents the dimension of the array and change it there. Such a constant must be declared before using it; this is usually done on the top section of the function that uses the array. Therefore, the declaration of the array above would be:
When initializing an array, you do not have to set the dimension of the array. You can just start initializing the members by their index. When you finish, the compiler would figure out the number of members in the array. After initializing the array, you do not have to access all of its members, you can just retrieve the values of those you need. Our SquareRoot array consists of 7 members, using the following program, you can display the values of less than 7 members:
This would produce:
Notice that although our array is made of 7 members, we asked the compiler to display only 4 of their values. You should always know the dimension of an array or at least have an approximate view of the number of its members. When accessing the members of an array, if you request values of members that are not of the group, the compiler would provide garbage values. Our SquareRoot array has 7 members. If you try to access more than 7 members, the result would be unpredictable:
This would produce:
Instead of guessing the dimension of an array, an alternative is to perform an operation that would find the dimension of the array, then use that dimension. To find the dimension of an array, use the sizeof operator. This is done by dividing the size of the array by the size of its data type. For example, suppose you have an array of integer numbers initialized as follows:
You can get the number of members of the array using the following operation:
Once you know the dimension of the array, you can use it as you see fit. For example, you can display it using a cout extractor:
Or you can use it wherever the array is involved:
|
|
||
Previous | Copyright © 2004-2010 FunctionX, Inc. | Next |
|