Home

Lists and Collections

 

The Array Class

 

Introduction

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.

The Length of an 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.

Processing an Array

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:

#using <mscorlib.dll>
using namespace System;

int main()
{
    // Initializing the whole array
    double SquareRoot[7] = {6.480, 8.306, 2.645, 20.149, 25.729, 3.00, 1.73};

    // Displaying the values of members of the array
    Console::WriteLine("Square Roots");
    for(int n = 0; n < 6; n++)
	Console::WriteLine("Root {0} = {1}", (n+1).ToString(), SquareRoot[n].ToString());

    Console::WriteLine();
    return 0;
}

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:

const int NbrOfPlayers = 6;
int HandballPlayers[NbrOfPlayers] = {22, 5, 12, 34, 2, 8};

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:

#using <mscorlib.dll>
using namespace System;

int main()
{
    // Initializing the whole array
    double SquareRoot[] = {6.480, 8.306, 2.645, 20.149, 25.729, 3.00, 1.73};

    // Displaying the values of members of the array
    Console::WriteLine("Square Roots");
    for(int n = 0; n < 4; n++)
	Console::WriteLine("Root {0} = {1}", (n+1).ToString(), SquareRoot[n].ToString());

    Console::WriteLine();
    return 0;
}

This would produce:

Square Roots
Root 1 = 6.48
Root 2 = 8.306
Root 3 = 2.645
Root 4 = 20.149

Press any key to continue...

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:

#using <mscorlib.dll>
using namespace System;

int main()
{
    // Initializing the whole array
    double SquareRoot[] = {6.480, 8.306, 2.645, 20.149, 25.729, 3.00, 1.73};

    // Displaying the values of members of the array
    Console::WriteLine("Square Roots");
    for(int n = 0; n < 12; n++)
	Console::WriteLine("Root {0} = {1}", (n+1).ToString(), SquareRoot[n].ToString());

    Console::WriteLine();
    return 0;
}

This would produce:

Square Roots
Root 1 = 6.48
Root 2 = 8.306
Root 3 = 2.645
Root 4 = 20.149
Root 5 = 25.729
Root 6 = 3
Root 7 = 1.73
Root 8 = 1.74251758607032E-307
Root 9 = 3.10892966796985E-308
Root 10 = 2.63708056194974E-308
Root 11 = 2.63718342118464E-308
Root 12 = 2.12407103416606E-314

Press any key to continue

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:

int BooksPages[] = { 122, 540, 38, 228, 96, 152, 304, 882 };

You can get the number of members of the array using the following operation:

int RecordSize = sizeof(BooksPages)/sizeof(int);

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:

Console::Write("Size of BooksPages = " << RecordSize << endl;

Or you can use it wherever the array is involved:

#using <mscorlib.dll>
using namespace System;

int main()
{
    // Initializing the whole array
    double SquareRoot[] = {6.480, 8.306, 2.645, 20.149, 25.729, 3.00, 1.73};
    int ArraySize = sizeof(SquareRoot)/sizeof(double);

    // Displaying the values of members of the array
    Console::WriteLine("Square Roots");
    for(int n = 0; n < ArraySize; n++)
	Console::WriteLine("Root {0} = {1}", (n+1).ToString(), SquareRoot[n].ToString());

    Console::WriteLine();
    return 0;
}

 

 


Previous Copyright © 2004-2010 FunctionX, Inc. Next