static void Main(string[] args) { #region ArrayList ArrayList aList = new ArrayList(); aList.Add("Bob"); aList.Add(46); Console.WriteLine("Count: {0}", aList.Count); Console.WriteLine("Capacity: {0}", aList.Capacity); ArrayList aList2 = new ArrayList(); aList2.AddRange(new object[] { "Kerrin", "Mellisa", "Louise", "Paul" }); aList.AddRange(aList2); aList2.Sort(); aList2.Reverse(); aList2.Insert(2, "Jim"); ArrayList range = aList2.GetRange(1, 3); foreach (object o in range) { Console.WriteLine(o); } foreach (object o in aList) { Console.WriteLine(o); } //aList2.RemoveAt(1); //aList2.RemoveRange(2, 3); Console.WriteLine("Kerrin Index : {0}", aList2.IndexOf("Kerrin", 0)); // convert arralist into array string[] myArray = (string[])aList2.ToArray(typeof(string)); string[] customers = { "One", "Two", "Three" }; ArrayList custArrayList = new ArrayList(); custArrayList.AddRange(customers); foreach (string s in custArrayList ) { Console.WriteLine(s); } #endregion Console.ReadLine(); }
static void Main(string[] args) { // Collections can resize unlike arrays // #region provides a way to collapse // long blocks of code // ---------- ARRAYLISTS ---------- #region ArrayList Code // ArrayLists are resizable arrays // that can hold multiple data types ArrayList aList = new ArrayList(); aList.Add("Bob"); aList.Add(40); // Number of items in list Console.WriteLine("ARRAYLIST\nCount: {0}", aList.Count); // The capacity automatically increases // as items are added Console.WriteLine("Capacity: {0}", aList.Capacity); ArrayList aList2 = new ArrayList(); // Add an object array aList2.AddRange(new object[] { "Mike", "Sally", "Egg" }); // Add 1 array list to another aList.AddRange(aList2); // You can sort the list if the types // are the same aList2.Sort(); aList2.Reverse(); // Insert at the 2nd position aList2.Insert(1, "Turkey"); // Get the 1st 2 items ArrayList range = aList2.GetRange(0, 2); /* * // Remove the first item * aList2.RemoveAt(0); * * // Remove the 1st 2 items * aList2.RemoveRange(0, 2); */ // Search for a match starting at the provided // index. You can also find the last index // with LastIndexOf Console.WriteLine("Turkey Index : {0}", aList2.IndexOf("Turkey", 0)); // Cycle through the list foreach (object o in range) { Console.WriteLine(o); } // Convert an ArrayList into a string array string[] myArray = (string[])aList2.ToArray(typeof(string)); // Convert a string array into an ArrayList string[] customers = { "Bob", "Sally", "Sue" }; ArrayList custArrayList = new ArrayList(); custArrayList.AddRange(customers); #endregion // ---------- DICTIONARIES ---------- #region Dictionary Code // Dictionaries store key value pairs // To create them define the data // type for the key and the value Dictionary <string, string> superheroes = new Dictionary <string, string>(); superheroes.Add("Clark Kent", "Superman"); superheroes.Add("Bruce Wayne", "Batman"); superheroes.Add("Barry West", "Flash"); // Remove a key / value superheroes.Remove("Barry West"); // Number of keys Console.WriteLine("\nDICTIONARY\nCount : {0}", superheroes.Count); // Check if a key is present Console.WriteLine("Clark Kent : {0}", superheroes.ContainsKey("Clark Kent")); // Get the value for the key and store it // in a string superheroes.TryGetValue("Clark Kent", out string test); Console.WriteLine($"Clark Kent : {test}"); // Cycle through key value pairs foreach (KeyValuePair <string, string> item in superheroes) { Console.WriteLine("{0} : {1}", item.Key, item.Value); } // Clear a dictionary superheroes.Clear(); #endregion // ---------- QUEUES ---------- #region Queue Code // Queue 1st in 1st Out Collection // Create a Queue Queue queue = new Queue(); // Add values queue.Enqueue(1); queue.Enqueue(2); queue.Enqueue(3); // Is item in queue Console.WriteLine("\nQUEUES\n1 in Queue : {0}", queue.Contains(1)); // Remove 1st item from queue Console.WriteLine("Remove 1 : {0}", queue.Dequeue()); // Look at 1st item but don't remove Console.WriteLine("Peek 1 : {0}", queue.Peek()); // Copy queue to array object[] numArray = queue.ToArray(); // Print array Console.WriteLine(string.Join(",", numArray)); // Print queue items foreach (object o in queue) { Console.WriteLine($"Queue : {o}"); } // Clear the queue queue.Clear(); #endregion // ---------- STACKS ---------- #region Queue Code // Stack Last in 1st Out Collection // Stacks are based on a stack of dishes that you wash in a restaurant // Create a stack Stack stack = new Stack(); // Put items on the stack stack.Push(1); stack.Push(2); stack.Push(3); // Get but don't remove item Console.WriteLine("\nSTACKS\nPeek 1 : {0}", stack.Peek()); // Remove item Console.WriteLine("Pop 1 : {0}", stack.Pop()); // Does item exist on stack Console.WriteLine("Contain 1 : {0}", stack.Contains(1)); // Copy stack to array object[] numArray2 = stack.ToArray(); // Print array Console.WriteLine(string.Join(",", numArray2)); // Print the stack foreach (object o in stack) { Console.WriteLine($"Stack : {o}"); } // Clear stack #endregion Console.ReadLine(); }
static void Main(string[] args) { ArrayList arr = new ArrayList(); int[] a = new int[] { 1, 3, 2, 6, 7, 4, 9, 10, 8, 5 }; arr.AddRange(a); Console.WriteLine("Given Arary:"); foreach (int i in arr) { Console.WriteLine("Values:" + i); } Console.WriteLine("\nCapacity: " + arr.Capacity); Console.WriteLine("Count: " + arr.Count); Console.WriteLine("IsfixedSize: " + arr.IsFixedSize); Console.WriteLine("IsReadOnly: " + arr.IsReadOnly); Console.WriteLine("\n"); Console.WriteLine("Inserted(1,6)"); arr.Insert(1, 6); arr.Remove(3); Console.WriteLine("Removed(3) Successfully worked "); foreach(int i in arr) { Console.WriteLine("Values:" + i); } Console.WriteLine("\n"); arr.RemoveAt(5); Console.WriteLine("RemovedAt(5) Successfully worked "); foreach (int i in arr) { Console.WriteLine("Values:" + i); } Console.WriteLine("\n"); Console.WriteLine("Sort List"); arr.Sort(); foreach (int i in arr) { Console.WriteLine("Values:" + i); } Console.WriteLine(); Console.WriteLine("Contains(11): " + arr.Contains(11)); Console.WriteLine("GetRange(2,4): " + arr.GetRange(2, 4)); Console.WriteLine("IndexOf(9): " + arr.IndexOf(9)); Console.WriteLine("\nToArray() "); arr.ToArray(); foreach (int i in arr) { Console.WriteLine("Values:" + i); } arr.Clear(); Console.WriteLine("Cleared Successfully"); Console.ReadKey(); }