示例#1
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();
        }
示例#2
0
        static void Main(string[] args)
        {
            //OBJECTS
            Car car1 = new Car();

            car1.Make  = "Oldsmobile";
            car1.Model = "Cutlas Supreme";
            car1.VIN   = "A1";

            Car car2 = new Car();

            car2.Make  = "Geo";
            car2.Model = "Prism";
            car2.VIN   = "B2";

            Book b1 = new Book();

            b1.Author = "Robert Tabor";
            b1.Title  = "Microsoft .NET XML Web Services";
            b1.ISBN   = "0-000-00000-0";


            //LISTS
            ArrayList myArrayList = new ArrayList();

            myArrayList.Add(car1);
            myArrayList.Add(car2);
            myArrayList.Add(b1);
            myArrayList.Remove(b1);
            foreach (Car car in myArrayList)
            {
                Console.WriteLine(car.Make);
            }

            List <Car> myList = new List <Car>();

            myList.Add(car1);
            myList.Add(car2);
            //myList.Add(b1); - ERROR


            foreach (Car car in myList)
            {
                Console.WriteLine(car.Model);
            }


            //DICTIONATY
            Dictionary <string, Car> myDictionary = new Dictionary <string, Car>();

            myDictionary.Add(car1.VIN, car1);
            myDictionary.Add(car2.VIN, car2);

            Console.WriteLine(myDictionary["B2"].Make);


            string[] names = { "Bob", "Steve", "Brian", "Chuck" };

            Car car3 = new Car()
            {
                Make = "BMW", Model = "750li", VIN = "C3"
            };
            Car car4 = new Car()
            {
                Make = "Toyota", Model = "4Runner", VIN = "D4"
            };



            //COLLECTION INITIALIZER
            List <Car> myList2 = new List <Car>()
            {
                new Car {
                    Make = "Oldsmobile", Model = "Cutlas Supreme", VIN = "E5"
                },
                new Car {
                    Make = "Nissan", Model = "Altima", VIN = "F6"
                }
            };

            Console.ReadLine();
        }
示例#3
0
        static void Main(string[] args)
        {
            //Arraylists
            Console.WriteLine("arraylists");
            ArrayList a1 = new ArrayList();

            a1.Add(5);
            a1.Add("raju");
            a1.Add(2.33);
            a1.Add('d');
            foreach (var val in a1)
            {
                Console.WriteLine(val);
            }
            a1.Remove(2.33);
            foreach (var val in a1)
            {
                Console.WriteLine(val);
            }
            Console.WriteLine("***********");

            //non generic Queue
            Console.WriteLine("Queue");
            Queue qlist = new Queue();

            qlist.Enqueue(1);
            qlist.Enqueue("raj");
            qlist.Enqueue('r');
            Console.WriteLine("removed element is" + qlist.Dequeue());
            foreach (var val in qlist)
            {
                Console.WriteLine(val);
            }
            Console.WriteLine("***********");

            Console.WriteLine("Stack");
            Stack slist = new Stack();

            slist.Push(23);
            slist.Push("hello");
            slist.Push(23.122);
            Console.WriteLine(slist.Pop());
            Console.WriteLine(slist.Peek());
            foreach (var s in slist)
            {
                Console.WriteLine(s);
            }
            Console.WriteLine("***********");

            Console.WriteLine("Hash table");
            Hashtable ht = new Hashtable();

            ht.Add(1, "raj");
            ht.Add("a", 1000);
            ht.Add("t1", 30);
            foreach (var h in ht.Keys)
            {
                Console.WriteLine(h);
                Console.WriteLine(ht[h]);
            }
            Console.WriteLine("***********");

            Console.WriteLine("List");
            List <int> list = new List <int>();

            list.Add(10);
            list.Add(10);
            list.Add('a');
            list.Add(34);
            foreach (int val in list)
            {
                Console.WriteLine(val);
            }
            Console.WriteLine("***********");

            HashSet <int> hs = new HashSet <int>();

            Console.WriteLine("Hash set");
            hs.Add(10);
            hs.Add(20);
            hs.Add(47);
            foreach (int val in hs)
            {
                Console.WriteLine(val);
            }
            Console.WriteLine("***********");

            Console.WriteLine(" generic queue");
            Queue <string> gqlist = new Queue <string>();

            gqlist.Enqueue("1");
            gqlist.Enqueue("arun");
            gqlist.Enqueue("c");
            Console.WriteLine(gqlist.Dequeue());
            foreach (var val in gqlist)
            {
                Console.WriteLine(val);
            }
            Stack <float> gslist = new Stack <float>();

            gslist.Push(23);
            gslist.Push(12.234f);
            gslist.Push(23.123f);
            Console.WriteLine(gslist.Pop());
            Console.WriteLine(gslist.Peek());
            foreach (var s in gslist)
            {
                Console.WriteLine(s);
            }
            Console.WriteLine("***********");

            Console.WriteLine("Dictionary");
            Dictionary <int, string> dt = new Dictionary <int, string>();

            dt.Add(1, "Raj");
            dt.Add(2, "radha");
            dt.Add(3, "lakshmi");
            foreach (KeyValuePair <int, string> kl in dt)
            {
                Console.WriteLine(kl.Key);
                Console.WriteLine(kl.Value);
            }
            Console.ReadLine();



            Console.ReadLine();
        }