Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Creates an and array and looks for the index of a given value from either end

/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Index.cs -- Creates an and array and looks for the index of a given //             value from either end. // //             Compile this program with the following command line: //                 C:>csc Index.cs // namespace nsArray {     using System;          public class Index     {         static public void Main ()         {             int [] Arr = new int [12]                         {29, 82, 42, 46, 54, 65, 50, 42, 5, 94, 19, 34};             Console.WriteLine ("The first occurrence of 42 is at index "                                + Array.IndexOf(Arr, 42));             Console.WriteLine ("The last occurrence of 42 is at index "                                + Array.LastIndexOf(Arr, 42));             int x = 0;             while ((x = Array.IndexOf (Arr, 42, x)) >= 0)             {                 Console.WriteLine ("42 found at index " + x);                 ++x;             }             x = Arr.Length - 1;             while ((x = Array.LastIndexOf (Arr, 42, x)) >= 0)             {                 Console.WriteLine ("42 found at index " + x);                 --x;             }         }     } }