Using for each to loop through an array : Array « Collections « Visual C++ .NET






Using for each to loop through an array

 
#include "stdafx.h"
using namespace System;

int main()
{
   array<int>^ array1 = gcnew array<int>(10){ 2, 7, 9, 6, 4, 1, 9, 5, 0, 10 };

   Array::Sort(array1);

   for each (int i in array1)
   {
      Console::Write("{0} ", i);
   }

   // Search for one of the values
   int index = Array::BinarySearch( array1, 115);

   if (index >= 0 )
      Console::WriteLine( "Found {0} at position {1}.", array1[index], index );
   else
      Console::WriteLine(" Not Found. ");
}

   
  








Related examples in the same category

1.Single dimension Arrays
2.Using gcnew to create array
3.Declare, create, and initialize a 1D native array
4.Declare, create, and initialize a 1D managed array
5.Declare, create, and initialize a 2D native array
6.Declare, create, and initialize a 2D managed array
7.Array initializing
8.Array for each
9.Shallow copy creates another name for the array
10.Using Array::Copy to copy array
11.Array equality test
12.Using array.Equals to test if two arrays are equal
13.class with managed array
14.Get array length
15.Sort array with Array.Sort
16.Using Array.BinarySearch to search an element in an array
17.Arrays iterators
18.Create an object for each object array position
19.Loop over the array with the interior pointer
20.Create an interior pointer to elements of the array
21.Array initialization without gcnew
22.Using gcnew to create array
23.Object array with non default constrctuctor
24.Assign value from native array to managed array
25.String arrays length
26.Use variables in the initializer list