Ejemplo n.º 1
0
        static void TestMyArray()
        {
            ArrayList a1 = new ArrayList()
            {
            };
            ArrayList2 a2 = new ArrayList2()
            {
            };

            Console.WriteLine($"capacities: {a1.Capacity} {a2.Capacity}");
            foreach (var i in new double[] { 12.2, 15.3, 16.8, 19.9 })
            {
                a1.Add(i);
                a2.Add(i);
            }
            Console.WriteLine($"counts: {a1.Count} {a2.Count}");
            Console.WriteLine($"capacities: {a1.Capacity} {a2.Capacity}");
            Console.WriteLine($"ToString: a1: {a1} a2: {a2}");
            a1.RemoveAt(1);
            a2.RemoveAt(1);
            Console.WriteLine($"Printed Arrays After Removal");
            for (int i = 0; i < a1.Count; i++)
            {
                Console.Write(a1[i] + " ");
            }
            Console.WriteLine("\n------\n");
            for (int i = 0; i < a2.Count; i++)
            {
                Console.Write(a2[i] + " ");
            }
            Console.WriteLine();
            a1.Clear();
            a2.Clear();
            Console.WriteLine("Both cleared now...");
            Console.WriteLine($"counts: {a1.Count} {a2.Count}");
            Console.WriteLine($"capacities: {a1.Capacity} {a2.Capacity}");
            for (int i = 0; i < a1.Count; i++)
            {
                Console.Write(a1[i] + " ");
            }
            Console.WriteLine("\n------\n");
            for (int i = 0; i < a2.Count; i++)
            {
                Console.Write(a2[i] + " ");
            }
            Console.WriteLine("\n------\n");
            Console.WriteLine("Adding 6 random numbers to each...");
            Random randomNumber = new Random();

            for (int i = 0; i < 6; i++)
            {
                int num = randomNumber.Next(1, 400);
                a1.Add(num);
                a2.Add(num);
            }
            Console.WriteLine($"counts: {a1.Count} {a2.Count}");
            Console.WriteLine($"capacities: {a1.Capacity} {a2.Capacity}");
            Console.WriteLine("Testing ForEach over ToArray():");
            foreach (var i in a1.ToArray())
            {
                Console.WriteLine(i);
            }
            foreach (var i in a1.ToArray())
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("Testing TrimToSize():");
            a1.TrimToSize();
            a2.TrimToSize();
            Console.WriteLine($"counts: {a1.Count} {a2.Count}");
            Console.WriteLine($"capacities: {a1.Capacity} {a2.Capacity}");
            foreach (var a in a1)
            {
                Console.Write(a + " ");
            }
            Console.WriteLine("\n------\n");
            foreach (var a in a2)
            {
                Console.Write(a + " ");
            }
            Console.WriteLine();
            Console.WriteLine("Testing Sort() when object type is Integer:");
            a1.Sort();
            a2.Sort();
            for (int i = 0; i < a1.Count; i++)
            {
                Console.Write(a1[i] + " ");
            }
            Console.WriteLine("\n------\n");
            for (int i = 0; i < a2.Count; i++)
            {
                Console.Write(a2[i] + " ");
            }
        }