static void Main(string[] args) { //declaring an int array MyGenericArray <int> intArray = new MyGenericArray <int>(5); //setting values for (int c = 0; c < 5; c++) { intArray.setItem(c, c * 5); } //retrieving the values for (int c = 0; c < 5; c++) { Console.Write(intArray.getItem(c) + " "); } Console.WriteLine(); //declaring a character array MyGenericArray <char> charArray = new MyGenericArray <char>(5); //setting values for (int c = 0; c < 5; c++) { charArray.setItem(c, (char)(c + 97)); } //retrieving the values for (int c = 0; c < 5; c++) { Console.Write(charArray.getItem(c) + " "); } Console.WriteLine(); Console.ReadKey(); }
static void Main(string[] args) { MyGenericArray <int> intArr = new MyGenericArray <int>(5); for (int c = 0; c < 5; c++) { intArr.SetItem(c, c * 5); } for (int c = 0; c < 5; c++) { Console.WriteLine(intArr.GetItem(c)); } Console.WriteLine(); MyGenericArray <char> charArr = new MyGenericArray <char>(5); for (int c = 0; c < 5; c++) { charArr.SetItem(c, (char)(c + 97)); } for (int c = 0; c < 5; c++) { Console.WriteLine(charArr.GetItem(c) + " "); } }
static void Main(string[] args) { // 声明一个整型数组 MyGenericArray <int> intArray = new MyGenericArray <int>(5); // 设置值 for (int c = 0; c < 5; c++) { intArray.setItem(c, c * 5); } // 获取值 for (int c = 0; c < 5; c++) { Console.Write(intArray.getItem(c) + " "); } Console.WriteLine(); // 声明一个字符数组 MyGenericArray <char> charArray = new MyGenericArray <char>(5); // 设置值 for (int c = 0; c < 5; c++) { charArray.setItem(c, (char)(c + 97));//char97,98,99,100,101 abcde } // 获取值 for (int c = 0; c < 5; c++) { Console.Write(charArray.getItem(c) + " "); } Console.WriteLine(); Console.ReadKey(); }
static void Main(string[] args) { //declaring an int array MyGenericArray <int> intArray = new MyGenericArray <int>(5); MyGenericArray <String> charArray = new MyGenericArray <String>(5); Console.Clear(); Console.WriteLine("1. Integer."); Console.WriteLine("2. String."); Console.Write("Select your choice: "); int choicer = Convert.ToInt16(Console.ReadLine()); while (true) { Console.WriteLine("\n\t****MENU****"); Console.WriteLine("1. Add an element"); Console.WriteLine("2. See the Top element."); Console.WriteLine("3. Remove top element."); Console.WriteLine("4. Display stack elements."); Console.WriteLine("5. Exit\n"); Console.Write("Select your choice: "); int choice = Convert.ToInt32(Console.ReadLine()); switch (choice) { case 1: Console.WriteLine("Enter an Element : "); if (choicer == 1) { intArray.Push(Convert.ToInt16(Console.ReadLine())); } else if (choicer == 2) { charArray.Push(Console.ReadLine()); } break; case 2: if (choicer == 1) { Console.WriteLine("Top element is: {0}", intArray.Peek()); } else if (choicer == 2) { Console.WriteLine("Top element is: {0}", charArray.Peek()); } break; case 3: if (choicer == 1) { Console.WriteLine("Element removed: {0}", intArray.Pop()); } else if (choicer == 2) { Console.WriteLine("Element removed: {0}", charArray.Pop()); } break; case 4: if (choicer == 1) { intArray.Display(); } else if (choicer == 2) { charArray.Display(); } break; case 5: System.Environment.Exit(1); break; } Console.ReadKey(); } }