Jagged Arrays |
|
Introduction |
So far, to declare a managed array, we entered its data type inside the <> operator. In reality, as we will learn in the next lesson, the section inside the <> operator is a placeholder for any valid data type. Because an array is primarily treated as its own (user-defined) data type, it can be used inside the <> operator. This means that you can use this operator to create an array inside of an array. An array created inside of another array is referred to as a jagged array. |
To create a jagged array, inside the <> operator, use the array keyword, its own <> operator, a data type and the ^ operator to create the new array. Here is an example: using namespace System; int main() { array<array<double> ^> ^ SingleFamilies = gcnew array<array<double> ^>(4); return 0; } This declaration indicates that you are creating an array named SingleFamilies and that array contains 4 elements. Each element of that array is itself an array of double-precision values. This declaration doesn't indicate how many elements each jagged array will contain. This means that each element of the SingleFamilies array can contain 1, 4, 100 or more elements. You will provide this information when you initialize each element of the SingleFamilies array.
As mentioned above, each element of the main array is itself an array. Therefore, before using it, you must initialize it. Because each element is declared as a handle, you must initialize it using the gcnew operator. To do this, access an element using its 0-based index and allocate memory for it using the gcnew operator. Also, because you are allocating memory for an array, you must specify its dimension. If an element is a one-dimensional array, specify its dimension as 1. This would be done as follows: using namespace System; int main() { array<array<double> ^> ^ SingleFamilies = gcnew array<array<double> ^>(2); SingleFamilies[0] = gcnew array<double>(1); SingleFamilies[1] = gcnew array<double>(1); return 0; } The above code only allocates memory for each element. It initializes its element(s) to 0 if the array is numeric-based. To access an element of a jagged array, you need at least two pairs of square brackets on the right side of the name of the variable. Inside the first square brackets, enter the index of the element from the main array. Inside the second square brackets, enter the index of the particular element you are trying to access. Here are examples: using namespace System; int main() { array<array<double> ^> ^ SingleFamilies = gcnew array<array<double> ^>(2); SingleFamilies[0] = gcnew array<double>(1); SingleFamilies[0][0] = 550550; SingleFamilies[1] = gcnew array<double>(1); SingleFamilies[1][0] =1215080; Console::WriteLine(L"Altair Realtors"); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Properties Listing"); Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-="); Console::WriteLine(L"Single Families"); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[0][0]); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[1][0]); Console::WriteLine(L"============================\n"); return 0; } This would produce: Altair Realtors =---------------------------= Properties Listing =--=--=--=--=--=--=-=-=-=-=-= Single Families Market Value: $550,550.00 Market Value: $1,215,080.00 ============================ Press any key to continue . . . Remember that each element of the main is its own but undefined array. This means that you can initialize each any way you want. This also means that one element of the main array can have a different number of elements from the others. As mentioned already, it is when allocating memory for an element of the jagged array that you decide what it would look like. Consider the following example: using namespace System; int main() { array<array<double> ^> ^ SingleFamilies = gcnew array<array<double> ^>(4); SingleFamilies[0] = gcnew array<double>(1); SingleFamilies[0][0] = 550550; SingleFamilies[1] = gcnew array<double>(1); SingleFamilies[1][0] = 1215080; SingleFamilies[2] = gcnew array<double>(3); SingleFamilies[2][0] = 722040; SingleFamilies[2][1] = 1450668; SingleFamilies[2][2] = 917297; SingleFamilies[3] = gcnew array<double>(2); SingleFamilies[3][0] = 325885; SingleFamilies[3][1] = 450735; Console::WriteLine(L"Altair Realtors"); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Properties Listing"); Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-="); Console::WriteLine(L"Silver Spring"); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[0][0]); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[1][0]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Potomac"); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[2][0]); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[2][1]); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[2][2]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Condominiums in DC"); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[3][0]); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[3][1]); Console::WriteLine(L"============================\n"); return 0; } In the above example, the first and the second elements of the main array each is an array that contains only one element. The third element of the main array contains 3 elements and the fourth contains 2 elements. The above code would produce: Altair Realtors =---------------------------= Properties Listing =--=--=--=--=--=--=-=-=-=-=-= Silver Spring Market Value: $550,550.00 Market Value: $1,215,080.00 =---------------------------= Potomac Market Value: $722,040.00 Market Value: $1,450,668.00 Market Value: $917,297.00 =---------------------------= Condominiums in DC Market Value: $325,885.00 Market Value: $450,735.00 ============================ Press any key to continue . . .
A jagged array can be created as two-dimensional. to create it, you can start with a normal single-dimensional array. Inside the <> operator, create a two-dimensional array by specifying its data type and its rank as 2. Here is an example: array<array<double, 2> ^> ^ MarketValues = gcnew array<array<double, 2> ^>(3); After allocating memory for the main array, you should do the same (allocate memory) for each element of the jagged array. When doing this, you must use the same formula you created the jagged array with inside the <> operator. Then, when allocating memory for each element of the jagged array, you can specify its ranks and how many elements it would have. This can be done as follows: using namespace System; int main() { array<array<double, 2> ^> ^ MarketValues = gcnew array<array<double, 2> ^>(3); // The first element of the main array is an array of // 2 lists and each list contains 3 elements MarketValues[0] = gcnew array<double, 2>(2, 3); // The second element of the main array is an array of // 4 lists and each list contains 1 element MarketValues[1] = gcnew array<double, 2>(4, 1); // The third element of the main array is an array of // 1 list and that list contains 5 elements MarketValues[2] = gcnew array<double, 2>(1, 5); return 0; } To access an element of a jagged array, you use at least two pairs of square brackets. In the fist pair, specify the rank of the current element from the main array: using namespace System; int main() { array<array<double, 2> ^> ^ MarketValues = gcnew array<array<double, 2> ^>(3); // The first element of the main array is an array of // 2 lists and each list contains 3 elements MarketValues[0] = gcnew array<double, 2>(2, 3); MarketValues[0][] // The second element of the main array is an array of // 4 lists and each list contains 1 element MarketValues[1] = gcnew array<double, 2>(4, 1); MarketValues[1][] return 0; } In the second square brackets, you apply the formula we used to access an element of a multi-dimensional array. Then you do what you want, such as assigning the desired value or retrieving the value of the element. Here are examples: using namespace System; int main() { array<array<double, 2> ^> ^ MarketValues = gcnew array<array<double, 2> ^>(3); // The first element of the main array is an array of // 2 lists and each list contains 3 elements MarketValues[0] = gcnew array<double, 2>(2, 3); MarketValues[0][0,0] = 1150760; MarketValues[0][0,1] = 665265; MarketValues[0][0,2] = 888500; MarketValues[0][1,0] = 450550; MarketValues[0][1,1] = 565265; MarketValues[0][1,2] = 475640; // The second element of the main array is an array of // 4 lists and each list contains 1 element MarketValues[1] = gcnew array<double, 2>(4, 1); MarketValues[1][0,0] = 250550; MarketValues[1][1,0] = 465265; MarketValues[1][2,0] = 1215080; MarketValues[1][3,0] = 388500; // The third element of the main array is an array of // 1 list and that list contains 5 elements MarketValues[2] = gcnew array<double, 2>(1, 5); MarketValues[2][0,0] = 725660; MarketValues[2][0,1] = 635605; MarketValues[2][0,2] = 285555; MarketValues[2][0,3] = 447650; MarketValues[2][0,4] = 277800; Console::WriteLine(L"Altair Realtors"); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Available Properties Listing"); Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-="); Console::WriteLine(L"Alexandria, VA"); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][0,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][0,2]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][1,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][1,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][1,2]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Washington, DC"); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][1,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][2,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][3,0]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Chevy Chase, MD"); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,2]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,3]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,4]); Console::WriteLine(L"============================\n"); return 0; } This would produce: Altair Realtors =---------------------------= Available Properties Listing =--=--=--=--=--=--=-=-=-=-=-= Alexandria, VA Market Value: $1,150,760.00 Market Value: $665,265.00 Market Value: $888,500.00 Market Value: $450,550.00 Market Value: $565,265.00 Market Value: $475,640.00 =---------------------------= Washington, DC Market Value: $250,550.00 Market Value: $465,265.00 Market Value: $1,215,080.00 Market Value: $388,500.00 =---------------------------= Chevy Chase, MD Market Value: $725,660.00 Market Value: $635,605.00 Market Value: $285,555.00 Market Value: $447,650.00 Market Value: $277,800.00 ============================ Press any key to continue . . .
The above array was two-dimensional. A jagged array can also be created as multi-dimensional. From the formula of the two-dimensional array, you simply change the rank from 2 to the desired rank. Here is an example: using namespace System; int main() { array<array<double, 3> ^> ^ MarketValues = gcnew array<array<double, 3> ^>(3); return 0; } To allocate memory for each jagged array, you must first use the same rank you specified when declaring the variable. Then, you must specify the rank and dimension that each particular jagged array will use. Here are examples: using namespace System; int main() { array<array<double, 3> ^> ^ MarketValues = gcnew array<array<double, 3> ^>(3); // The first element of the main array is an array of // 2 lists. Each list will contain 3 sub-lists // Each sub-list will contain 1 element MarketValues[0] = gcnew array<double, 3>(2, 3, 1); // The second element of the main array is an array of // 4 lists. Each list will contain 1 sub-list // The sub-list will contain 2 elements MarketValues[1] = gcnew array<double, 3>(4, 1, 2); // The third element of the main array is an array of // 1 list. The list will contain 2 sub-lists // Each sub-list will contain 5 elements MarketValues[2] = gcnew array<double, 3>(1, 2, 5); return 0; } To access an element of the jagged array, you use two pairs of square brackets. In the fist pair, specify the rank of an element from the main array. In the second square brackets, enter the index of the first dimension of the jagged array, followed by a comma, followed by the appropriate index of the second dimension, followed by a comma, and followed by an index corresponding to the last dimension. You can then you do what you want, including assigning the desired value or retrieving the value of the element. Here are examples: using namespace System; int main() { array<array<double, 3> ^> ^ MarketValues = gcnew array<array<double, 3> ^>(3); // The first element of the main array is an array of // 2 lists. Each list contains 3 sub-lists // Each sub-list contains 1 element MarketValues[0] = gcnew array<double, 3>(2, 3, 1); MarketValues[0][0,0,0] = 1150760; MarketValues[0][0,1,0] = 665265; MarketValues[0][0,2,0] = 888500; MarketValues[0][1,0,0] = 450550; MarketValues[0][1,1,0] = 565265; MarketValues[0][1,2,0] = 475640; // The second element of the main array is an array of // 4 lists. Each list contains 1 sub-list // The sub-list contains 2 elements MarketValues[1] = gcnew array<double, 3>(4, 1, 2); MarketValues[1][0,0,0] = 250550; MarketValues[1][0,0,1] = 465265; MarketValues[1][1,0,0] = 1215080; MarketValues[1][1,0,1] = 388500; MarketValues[1][2,0,0] = 255550; MarketValues[1][2,0,1] = 464235; MarketValues[1][3,0,0] = 2365770; MarketValues[1][3,0,1] = 730458; // The third element of the main array is an array of // 1 list. The list contains 2 sub-lists // Each sub-list contains 5 elements MarketValues[2] = gcnew array<double, 3>(1, 2, 5); MarketValues[2][0,0,0] = 725660; MarketValues[2][0,1,1] = 635605; MarketValues[2][0,0,2] = 285555; MarketValues[2][0,1,3] = 447650; MarketValues[2][0,0,4] = 277800; MarketValues[2][0,1,0] = 7345665; MarketValues[2][0,0,1] = 5555565; MarketValues[2][0,1,2] = 2225055; MarketValues[2][0,0,3] = 645825; MarketValues[2][0,1,4] = 707650; Console::WriteLine(L"Altair Realtors"); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Available Properties Listing"); Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-="); Console::WriteLine(L"Virginia"); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][0,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][0,1,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][0,2,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][1,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][1,1,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][1,2,0]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Washington, DC"); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][0,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][0,0,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][1,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][1,0,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][2,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][2,0,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][3,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][3,0,1]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Maryland"); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,2]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,3]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,4]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,2]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,3]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,4]); Console::WriteLine(L"============================\n"); return 0; } This would produce: Altair Realtors =---------------------------= Available Properties Listing =--=--=--=--=--=--=-=-=-=-=-= Virginia Market Value: $1,150,760.00 Market Value: $665,265.00 Market Value: $888,500.00 Market Value: $450,550.00 Market Value: $565,265.00 Market Value: $475,640.00 =---------------------------= Washington, DC Market Value: $250,550.00 Market Value: $465,265.00 Market Value: $1,215,080.00 Market Value: $388,500.00 Market Value: $255,550.00 Market Value: $464,235.00 Market Value: $2,365,770.00 Market Value: $730,458.00 =---------------------------= Maryland Market Value: $725,660.00 Market Value: $635,605.00 Market Value: $285,555.00 Market Value: $447,650.00 Market Value: $277,800.00 Market Value: $7,345,665.00 Market Value: $5,555,565.00 Market Value: $2,225,055.00 Market Value: $645,825.00 Market Value: $707,650.00 ============================ Press any key to continue . . .
|
|
||
Home | Copyright © 2007-2013, FunctionX | Home |
|