public static MyArrayList <int> AddArray() { MyArrayList <int> arrayList; Console.Write("Enter array size : "); int size = int.Parse(Console.ReadLine()); arrayList = new MyArrayList <int>(size); Console.WriteLine("Enter elements, each on a new line. Type 'stop' and press enter to stop."); string input; while (!arrayList.IsFull()) { input = Console.ReadLine(); if (input.Equals("stop")) { break; } arrayList.Add(int.Parse(input)); } return(arrayList); }
public static MyArrayList <T> Merge(MyArrayList <T> arrayList1, MyArrayList <T> arrayList2) { MyArrayList <T> mergedList = new MyArrayList <T>(arrayList1.Length + arrayList2.Length); int j = 0, k = 0; while (j <= arrayList1.Last && k <= arrayList2.Last) { if (arrayList1.array[j].CompareTo(arrayList2.array[k]) <= 0) { mergedList.Add(arrayList1.array[j]); j++; } else { mergedList.Add(arrayList2.array[k]); k++; } } if (j > arrayList1.Last) { while (k <= arrayList2.Last) { mergedList.Add(arrayList2.array[k]); k++; } } else { while (j <= arrayList1.Last) { mergedList.Add(arrayList1.array[j]); j++; } } return(mergedList); }
public static void Select(int option) { int index, item; switch ((MenuOption)option) { case MenuOption.AddArray: arrayList = MyArrayList <int> .AddArray(); break; case MenuOption.Traverse: arrayList.Traverse(); Menu.MenuOrExit(); break; case MenuOption.Insert: Console.Write("Enter index : "); index = int.Parse(Console.ReadLine()); Console.Write("\nEnter element : "); item = int.Parse(Console.ReadLine()); arrayList.Insert(item, index); Menu.MenuOrExit(); break; case MenuOption.Delete: Console.Write("Enter index : "); index = int.Parse(Console.ReadLine()); arrayList.Delete(index); Menu.MenuOrExit(); break; case MenuOption.Sort: arrayList.Sort(); Menu.MenuOrExit(); break; case MenuOption.Search: Console.Write("Enter item : "); item = int.Parse(Console.ReadLine()); arrayList.Search(item); Menu.MenuOrExit(); break; case MenuOption.BinarySearch: Console.Write("Enter item : "); item = int.Parse(Console.ReadLine()); arrayList.BinarySearch(item); Menu.MenuOrExit(); break; case MenuOption.Merge: MyArrayList <int> arrayList1; Console.WriteLine("Enter second array to merge with : "); Console.Write("Enter array size : "); int size = int.Parse(Console.ReadLine()); arrayList1 = new MyArrayList <int>(size); Console.WriteLine("Enter elements, each on a new line."); string input = Console.ReadLine(); while (!arrayList1.IsFull()) { arrayList1.Add(int.Parse(input)); input = Console.ReadLine(); } arrayList = MyArrayList <int> .Merge(arrayList, arrayList1); Menu.MenuOrExit(); break; } }