示例#1
0
        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();
        }
示例#2
0
        static void Main(string[] args)

        {
            // ARRAYLIST
            // Creates and initializes a new ArrayList.
            // Can hold different types of data
            // Use Add/Remove to insert/delete elements

            ArrayList myAL = new ArrayList();

            myAL.Add("HelloAgain");
            myAL.Add("!");
            myAL.Add("World");
            myAL.Add("!");
            myAL.Add("Hello There");
            //myAL.Add(1);
            //myAL.Add(true);

            bool bContains = myAL.Contains("!");
            int  hereItIs  = myAL.IndexOf("Hello"); // returns -1
            int  hereItIs2 = myAL.LastIndexOf("!");

            myAL.Sort();

            // Displays the properties and values of the ArrayList.
            Console.WriteLine("myAL");
            Console.WriteLine("    Count:    {0}", myAL.Count);
            Console.WriteLine("    Capacity: {0}", myAL.Capacity);
            Console.Write("    Values:");
            PrintValues(myAL);

            // Sorting with IComparable

            // create and initalize new ArrayList, i.e. mylist
            ArrayList mylist = new ArrayList();

            mylist.Add("Welcome");
            mylist.Add("to");
            mylist.Add("geeks");
            mylist.Add("for");
            mylist.Add("geeks");
            mylist.Add("2");

            IComparer Comp1 = new myClass();

            // sort the value of ArrayList
            // using Sort(IComparer) method
            mylist.Sort(Comp1);

            foreach (Object ob in mylist)
            {
                Console.WriteLine(ob);
            }


            // HASHTABLE
            // Create a new hash table.
            // Uses Add/Remove
            Hashtable openWith = new Hashtable();

            // Add some elements to the hash table. There are no
            // duplicate keys, but some of the values are duplicates.
            openWith.Add("txt", "notepad.exe");
            openWith.Add("bmp", "paint.exe");
            openWith.Add("dib", "paint.exe");
            openWith.Add("rtf", "wordpad.exe");

            // The Add method throws an exception if the new key is
            // already in the hash table.
            try
            {
                openWith.Add("txt", "winword.exe");
            }
            catch
            {
                Console.WriteLine("An element with Key = \"txt\" already exists.");
            }
        }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Collections!\n");

            // *********  ArrayList  **************

            ArrayList al = new ArrayList
            {
                "Hello",
                "Collection",
                "from me :)"
            };

            Console.WriteLine("The array has " + al.Count + "items:");

            foreach (var item in al)
            {
                Console.WriteLine(item.ToString());
            }

            //  Hello Collections!

            //  The array has 4items:
            //  Hello
            //  Collection
            //  from me :)

            al.Sort();

            Console.WriteLine("\nSorting result:");

            foreach (var item in al)
            {
                Console.WriteLine(item.ToString());
            }

            al.Sort(new MyReverseSort());

            Console.WriteLine("\nCustom reverse sorting result:");

            foreach (var item in al)
            {
                Console.WriteLine(item.ToString());
            }

            al.Reverse();
            Console.WriteLine("\nReverse sorting result:");

            foreach (var item in al)
            {
                Console.WriteLine(item.ToString());
            }


            // ****************************************

            Dictionary <int, string> keyValuePairs = new Dictionary <int, string>
            {
                { 101, "Hello error" },
                { 202, "Another error" }
            };

            Dictionary <int, string> keyValuePairs1 = new Dictionary <int, string>
            {
                [101] = "Hello error",
                [203] = "Another error"
            };

            Hashtable hashtable = new Hashtable();

            hashtable.Add(1, 23);
            hashtable.Add(2, 24);
        }
示例#4
0
        static void Main(string[] args)
        {
            #region ArrayList Sample

            ArrayList list = new ArrayList();

            list.Add(5);
            list.AddRange(new int[] { 2, 3, 4 });

            Console.WriteLine(list.IndexOf(3));

            list.Sort();
            list.Reverse();

            Display(list);

            #endregion

            #region Hashtable Sample

            Hashtable map = new Hashtable();

            map.Add("Mattan", 9);
            map.Add("Ilan", 23);
            map.Add("Boris", 49);

            foreach (string name in map.Keys)
            {
                Console.WriteLine(name + "=" + map[name]);
            }

            int mattanAge = (int)map["Mattan"];
            map["Mattan"] = mattanAge + 1;

            int ageSum = 0;
            foreach (int age in map.Values)
            {
                ageSum += age;
            }

            #endregion

            #region Queue and Stack Sample

            Queue queue = new Queue();

            queue.Enqueue("A");
            queue.Enqueue("B");
            queue.Enqueue("C");

            Display(queue);

            Stack stack = new Stack();

            stack.Push("A");
            stack.Push("B");
            stack.Push("C");

            Display(stack);

            #endregion
        }
示例#5
0
        static void Main(string[] args)
        {
            //test object
            Ellipse testEllipse = new Ellipse(4.4, 1.6, 5.3);

            //ADD ELEMENT  -- ArrayList vs LinkedList
            Stopwatch stopWatch = new Stopwatch();
            Random    rand      = new Random();
            ArrayList array     = new ArrayList();

            stopWatch.Start();
            for (int i = 0; i < 500000; i++)
            {
                array.Add(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1)));
            }
            array.Add(testEllipse);
            for (int i = 0; i < 500000; i++)
            {
                array.Add(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1)));
            }
            stopWatch.Stop();
            Console.WriteLine("Add Elements: TestTime for ArrayList - " + stopWatch.Elapsed);
            stopWatch.Reset();


            LinkedList <Ellipse> linkedList = new LinkedList <Ellipse>();

            stopWatch.Start();
            for (int i = 0; i < 500000; i++)
            {
                linkedList.AddLast(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1)));
            }
            linkedList.AddLast(testEllipse);
            for (int i = 0; i < 500000; i++)
            {
                linkedList.AddLast(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1)));
            }
            stopWatch.Stop();
            Console.WriteLine("Add Elements: TestTime for LinkedList - " + stopWatch.Elapsed);
            stopWatch.Reset();

            Ellipse result = new Ellipse();

            //SEARCH ELEMENT --  ArrayList vs LinkedList
            stopWatch.Start();
            array.Sort(new CustomEllipseComparer());
            int index = array.BinarySearch((object)testEllipse, new CustomEllipseComparer());

            result = (Ellipse)array[index];
            stopWatch.Stop();
            Console.WriteLine("\nSearch Elements: TestTime for ArrayList - " + stopWatch.Elapsed);
            stopWatch.Reset();

            stopWatch.Start();
            result = (Ellipse)linkedList.Find(testEllipse).Value;
            stopWatch.Stop();
            Console.WriteLine("Search Elements: TestTime for LinkedList - " + stopWatch.Elapsed);
            stopWatch.Reset();

            //REMOVE ELEMENT --  ArrayList vs LinkedList
            stopWatch.Start();
            array.Remove(testEllipse);
            stopWatch.Stop();
            Console.WriteLine("\nRemove Elements: TestTime for ArrayList - " + stopWatch.Elapsed);
            stopWatch.Reset();

            stopWatch.Start();
            linkedList.Remove(testEllipse);
            stopWatch.Stop();
            Console.WriteLine("Remove Elements: TestTime for LinkedList - " + stopWatch.Elapsed);
            stopWatch.Reset();

            //ADD ELEMENT  -- Stack vs Queue
            Stack <Ellipse> stack = new Stack <Ellipse>();

            stopWatch.Start();
            for (int i = 0; i < 5000; i++)
            {
                stack.Push(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1)));
            }
            stack.Push(testEllipse);
            for (int i = 0; i < 5000; i++)
            {
                stack.Push(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1)));
            }
            stopWatch.Stop();
            Console.WriteLine("\nAdd Elements: TestTime for Stack - " + stopWatch.Elapsed);
            stopWatch.Reset();

            Queue <Ellipse> queue = new Queue <Ellipse>();

            stopWatch.Start();
            for (int i = 0; i < 5000; i++)
            {
                queue.Enqueue(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1)));
            }
            queue.Enqueue(testEllipse);
            for (int i = 0; i < 5000; i++)
            {
                queue.Enqueue(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1)));
            }
            stopWatch.Stop();
            Console.WriteLine("Add Elements: TestTime for Queue - " + stopWatch.Elapsed);
            stopWatch.Reset();


            //SEARCH ELEMENT --  Stack vs Queue
            CustomEllipseComparer circleComparer = new CustomEllipseComparer();

            stopWatch.Start();
            for (int i = 0; i < 10000; i++)
            {
                if (circleComparer.Compare(testEllipse, stack.ElementAt(i)) == 0)
                {
                    result = stack.ElementAt(i);
                }
            }
            stopWatch.Stop();
            Console.WriteLine("\nSearch Elements: TestTime for Stack - " + stopWatch.Elapsed);
            stopWatch.Reset();

            stopWatch.Start();
            for (int i = 0; i < 10000; i++)
            {
                if (circleComparer.Compare(testEllipse, queue.ElementAt(i)) == 0)
                {
                    result = queue.ElementAt(i);
                }
            }
            stopWatch.Stop();
            Console.WriteLine("Search Elements: TestTime for Queue - " + stopWatch.Elapsed);
            stopWatch.Reset();


            //REMOVE ELEMENT --  Stack vs Queue
            stopWatch.Start();
            stack.Pop();
            stopWatch.Stop();
            Console.WriteLine("\nRemove Elements: TestTime for Stack - " + stopWatch.Elapsed);
            stopWatch.Reset();

            stopWatch.Start();
            queue.Dequeue();
            stopWatch.Stop();
            Console.WriteLine("Remove Elements: TestTime for Queue - " + stopWatch.Elapsed);
            stopWatch.Reset();

            //ADD ELEMENT  -- HashTable vs Dictionary
            Dictionary <int, Ellipse> dictionary = new Dictionary <int, Ellipse>();
            Hashtable hashTable = new Hashtable();

            stopWatch.Start();
            for (int i = 0; i < 1000000; i++)
            {
                hashTable.Add(i, new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1)));
            }
            stopWatch.Stop();
            Console.WriteLine("\nAdd Elements: TestTime for HashTable - " + stopWatch.Elapsed);
            stopWatch.Reset();


            stopWatch.Start();
            for (int i = 0; i < 1000000; i++)
            {
                dictionary.Add(i, new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1)));
            }
            stopWatch.Stop();
            Console.WriteLine("Add Elements: TestTime for Dictionary - " + stopWatch.Elapsed);
            stopWatch.Reset();

            //SEARCH ELEMENT --  HashTable vs Dictionary
            stopWatch.Start();
            result = (Ellipse)hashTable[500000];
            stopWatch.Stop();
            Console.WriteLine("\nSearch Elements: TestTime for Hashtable - " + stopWatch.Elapsed);
            stopWatch.Reset();

            stopWatch.Start();
            result = (Ellipse)dictionary[500000];
            stopWatch.Stop();
            Console.WriteLine("Search Elements: TestTime for Dictionary - " + stopWatch.Elapsed);
            stopWatch.Reset();


            //REMOVE ELEMENT  -- HashTable vs Dictionary
            stopWatch.Start();
            hashTable.Remove(500000);
            stopWatch.Stop();
            Console.WriteLine("\nRemove Elements: TestTime for HashTable - " + stopWatch.Elapsed);
            stopWatch.Reset();

            stopWatch.Start();
            dictionary.Remove(500000);
            stopWatch.Stop();
            Console.WriteLine("Remove Elements: TestTime for Dictionary - " + stopWatch.Elapsed);
            stopWatch.Reset();


            Console.WriteLine("\nAll operations have ended. Press enter to exit");
            Console.ReadLine();
        }