static void Main(string[] args) { GenericList <int> list = new GenericList <int>(10); list.Add(10); list.Add(20); list.Add(14); //list.Insert(3, 1); //list.RemoveAt(1); list.Add(1); list.Insert(100, 0); Console.WriteLine("Index of 1: " + list.IndexOf(1)); //Console.WriteLine(list.IndexOf(14)); Console.WriteLine("List length: " + list.Length); for (int i = 0; i < list.Length; i++) { Console.WriteLine(list[i]); } }
public static void TestMethod() { // Make an instance - test constructors GenericList <int> list = new GenericList <int>(); // Print capacity and count - test properties Console.WriteLine("capacity = {0}", list.Capacity); // capacity = 4 Console.WriteLine("count = {0}", list.Count); // count = 0 // Add items - test Add(T elem) method for (int i = 0; i < 10; i++) { list.Add(i); } // Print the list and list.Count - test ToString() method and count functionality Console.WriteLine(list); // 0 1 2 3 4 5 6 7 8 9 Console.WriteLine("count = {0}", list.Count); // 10 // Remove element by index - test RemoveAt(int index) method and reverse count functionality list.RemoveAt(3); // 0 1 2 3 4 5 6 7 8 9 -> 0 1 2 4 5 6 7 8 9 Console.WriteLine(list); // 0 1 2 4 5 6 7 8 9 Console.WriteLine("count = {0}", list.Count); // count = 9 Console.WriteLine("capacity = {0}", list.Capacity); // capacity = 16 - test the auto-grow functionality // Insert element at given index - test Insert(int index, T element) method list.Insert(3, -5); // 0 1 2 4 5 6 7 8 9 -> 0 1 2 -5 4 5 6 7 8 9 Console.WriteLine(list); // 0 1 2 -5 4 5 6 7 8 9 Console.WriteLine("count = {0}", list.Count); // cout = 10 // Print the list with ElementAt(int index) method - test ElementAt(int index) method for (int i = 0; i < list.Count; i++) { Console.Write("{0} ", list.ElementAt(i)); // 0 1 2 -5 4 5 6 7 8 9 } Console.WriteLine(); // Test FindByValue(T value) method Console.WriteLine(list.FindByValue(8)); // 8 - value 8 is at the 8th index Console.WriteLine(list.FindByValue(10)); // -1 - 10 is not in the list // Test Min() and Max() methods Console.WriteLine("min = {0}", list.Min()); // min = -5 Console.WriteLine("max = {0}", list.Max()); // max = 9 // Test Clear() method Console.WriteLine(list); // 0 1 2 -5 4 5 6 7 8 9 list.Clear(); Console.WriteLine(list + "(nothing)"); // (nothing) }