public void InsertAt(int index, T newElement) { if (index < 0 || index > this.elements.Length - 1) { throw new IndexOutOfRangeException("The index you are trying to access is outside of the boundaries of the array"); } GenericList <T> temp = new GenericList <T>(this.capacity); for (int i = 0; i < elements.Length; i++) { if (i == index) { temp.Add(newElement); } temp.Add(this.elements[i]); } this.elements = temp.Elements; }
public static void Test() { //Creating a generic list GenericList <int> test = new GenericList <int>(10); for (int i = 0; i < 10; i++) { test.Add(i); } Console.WriteLine("A generic list:"); Console.WriteLine("Result: " + test); Console.WriteLine(); // Testing methods Console.WriteLine("Remove index 2:"); test.RemoveAt(2); Console.WriteLine("Result: " + test); Console.WriteLine(); Console.WriteLine("Insert at index 2 - 100:"); test.InsertAt(2, 100); Console.WriteLine("Result: " + test); Console.WriteLine(); Console.WriteLine("Get the index of number 3: "); Console.WriteLine("3 is at index: " + test.GetIndexOf(3)); Console.WriteLine(); Console.WriteLine("Get the list's length:"); Console.WriteLine(test.Length()); Console.WriteLine(); Console.WriteLine("Get the element at index 2: "); Console.WriteLine(test.ElementAt(2)); Console.WriteLine(); Console.WriteLine("Get the min element:"); Console.WriteLine(test.Min()); Console.WriteLine(); Console.WriteLine("Get the max element:"); Console.WriteLine(test.Max()); Console.WriteLine(); Console.WriteLine("Clear all the list:"); test.ClearAll(); Console.WriteLine("Result: " + test); }