11
22// <Snippet1>
33using namespace System ;
4- void main1 ();
5- void main2 ();
6- void main ()
7- {
8- main1 ();
9- Console::WriteLine ();
10- main2 ();
11- }
124
13- void PrintValues ( Array^ myArr );
14- void main1 ()
5+ void main ()
156{
167 // Creates and initializes two new Array instances.
17- Array^ mySourceArray = Array::CreateInstance ( String::typeid , 6 );
18- mySourceArray->SetValue ( " three" , 0 );
19- mySourceArray->SetValue ( " napping" , 1 );
20- mySourceArray->SetValue ( " cats" , 2 );
21- mySourceArray->SetValue ( " in" , 3 );
22- mySourceArray->SetValue ( " the" , 4 );
23- mySourceArray->SetValue ( " barn" , 5 );
24- Array^ myTargetArray = Array::CreateInstance ( String::typeid , 15 );
25- myTargetArray->SetValue ( " The" , 0 );
26- myTargetArray->SetValue ( " quick" , 1 );
27- myTargetArray->SetValue ( " brown" , 2 );
28- myTargetArray->SetValue ( " fox" , 3 );
29- myTargetArray->SetValue ( " jumps" , 4 );
30- myTargetArray->SetValue ( " over" , 5 );
31- myTargetArray->SetValue ( " the" , 6 );
32- myTargetArray->SetValue ( " lazy" , 7 );
33- myTargetArray->SetValue ( " dog" , 8 );
8+ Array^ mySourceArray = Array::CreateInstance (String::typeid , 6 );
9+ mySourceArray->SetValue (" three" , 0 );
10+ mySourceArray->SetValue (" napping" , 1 );
11+ mySourceArray->SetValue (" cats" , 2 );
12+ mySourceArray->SetValue (" in" , 3 );
13+ mySourceArray->SetValue (" the" , 4 );
14+ mySourceArray->SetValue (" barn" , 5 );
15+ Array^ myTargetArray = Array::CreateInstance (String::typeid , 15 );
16+ myTargetArray->SetValue (" The" , 0 );
17+ myTargetArray->SetValue (" quick" , 1 );
18+ myTargetArray->SetValue (" brown" , 2 );
19+ myTargetArray->SetValue (" fox" , 3 );
20+ myTargetArray->SetValue (" jumps" , 4 );
21+ myTargetArray->SetValue (" over" , 5 );
22+ myTargetArray->SetValue (" the" , 6 );
23+ myTargetArray->SetValue (" lazy" , 7 );
24+ myTargetArray->SetValue (" dog" , 8 );
3425
3526 // Displays the values of the Array.
36- Console::WriteLine ( " The target Array instance contains the following (before and after copying):" );
37- PrintValues ( myTargetArray );
27+ Console::WriteLine ( " The target Array instance contains the following (before and after copying):" );
28+ PrintValues (myTargetArray);
3829
3930 // Copies the source Array to the target Array, starting at index 6.
40- mySourceArray->CopyTo ( myTargetArray, 6 );
31+ mySourceArray->CopyTo (myTargetArray, 6 );
4132
4233 // Displays the values of the Array.
43- PrintValues ( myTargetArray );
34+ PrintValues (myTargetArray);
4435}
4536
46- void PrintValues ( Array^ myArr )
37+ void PrintValues (Array^ myArr)
4738{
4839 System::Collections::IEnumerator^ myEnumerator = myArr->GetEnumerator ();
4940 int i = 0 ;
50- int cols = myArr->GetLength ( myArr->Rank - 1 );
51- while ( myEnumerator->MoveNext () )
41+ int cols = myArr->GetLength (myArr->Rank - 1 );
42+ while (myEnumerator->MoveNext ())
5243 {
53- if ( i < cols )
44+ if (i < cols)
5445 {
5546 i++;
5647 }
@@ -60,13 +51,12 @@ void PrintValues( Array^ myArr )
6051 i = 1 ;
6152 }
6253
63- Console::Write ( " {0}" , myEnumerator->Current );
54+ Console::Write ( " {0}" , myEnumerator->Current );
6455 }
6556
6657 Console::WriteLine ();
6758}
6859
69-
7060/*
7161 This code produces the following output.
7262
@@ -75,63 +65,3 @@ void PrintValues( Array^ myArr )
7565 The quick brown fox jumps over three napping cats in the barn
7666 */
7767// </Snippet1>
78- // <Snippet2>
79- void PrintIndexAndValues ( Array^ myArray );
80- void main2 ()
81- {
82- // Creates and initializes the source Array.
83- Array^ myArrayZero = Array::CreateInstance ( String::typeid , 3 );
84- myArrayZero->SetValue ( " zero" , 0 );
85- myArrayZero->SetValue ( " one" , 1 );
86-
87- // Displays the source Array.
88- Console::WriteLine ( " The array with lowbound=0 contains:" );
89- PrintIndexAndValues ( myArrayZero );
90-
91- // Creates and initializes the target Array.
92- array<int >^myArrLen = {4 };
93- array<int >^myArrLow = {2 };
94- Array^ myArrayTwo = Array::CreateInstance ( String::typeid , myArrLen, myArrLow );
95- myArrayTwo->SetValue ( " two" , 2 );
96- myArrayTwo->SetValue ( " three" , 3 );
97- myArrayTwo->SetValue ( " four" , 4 );
98- myArrayTwo->SetValue ( " five" , 5 );
99-
100- // Displays the target Array.
101- Console::WriteLine ( " The array with lowbound=2 contains:" );
102- PrintIndexAndValues ( myArrayTwo );
103-
104- // Copy from the array with lowbound=0 to the array with lowbound=2.
105- myArrayZero->CopyTo ( myArrayTwo, 3 );
106-
107- // Displays the modified target Array.
108- Console::WriteLine ( " \n After copying at relative index 1:" );
109- PrintIndexAndValues ( myArrayTwo );
110- }
111-
112- void PrintIndexAndValues ( Array^ myArray )
113- {
114- for ( int i = myArray->GetLowerBound ( 0 ); i <= myArray->GetUpperBound ( 0 ); i++ )
115- Console::WriteLine ( " \t [{0}]:\t {1}" , i, myArray->GetValue ( i ) );
116- }
117-
118- /*
119- This code produces the following output.
120-
121- The array with lowbound=0 contains:
122- [0]: zero
123- [1]: one
124- [2]:
125- The array with lowbound=2 contains:
126- [2]: two
127- [3]: three
128- [4]: four
129- [5]: five
130-
131- After copying at relative index 1:
132- [2]: two
133- [3]: zero
134- [4]: one
135- [5]:
136- */
137- // </Snippet2>
0 commit comments