Exemple #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Создаем пустую коллекцию:");
            var arr = new DynamicArray <int>();

            Print(arr);


            Console.WriteLine("Добавляем 12 чисел:");
            for (int i = 0; i < 12; i++)
            {
                arr.Add(i);
            }
            Print(arr);

            Console.WriteLine("Добавляем массив чисел:");
            arr.AddRange(new int[] { 9, 8, 7, 6, 5, 4, 3 });
            Print(arr);

            Console.WriteLine("Удаляем элемент со значением 2:");
            arr.Remove(2);
            Print(arr);

            Console.WriteLine("Удаляем элемент со значением 5:");
            arr.Remove(5);
            Print(arr);

            Console.WriteLine("Удаляем элемент со значением 9:");
            arr.Remove(9);
            Print(arr);


            Console.WriteLine("Добавляем элемент -1 на 2 позицию:");
            arr.Insert(-1, 2);
            Print(arr);

            Console.WriteLine("Добавляем элемент -1 на 10 позицию:");
            arr.Insert(-1, 10);
            Print(arr);

            Console.WriteLine("Добавляем элемент -1 на 10 позицию:");
            arr.Insert(-1, 10);
            Print(arr);

            Console.WriteLine("Добавляем элемент -1 на 10 позицию:");
            arr.Insert(-1, 10);
            Print(arr);

            Console.WriteLine("Добавляем элемент в начало:");
            arr.Insert(-100, 0);
            Print(arr);


            Console.ReadKey();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            try
            {
                DynamicArray <int> dinamicArray = new DynamicArray <int>();
                Console.WriteLine($"Length = { dinamicArray.Length}, Capacity = { dinamicArray.Capacity}");

                dinamicArray = new DynamicArray <int>(2);
                Console.WriteLine($"Length = { dinamicArray.Length}, Capacity = { dinamicArray.Capacity}");

                int[] array = { 1, 2 };

                dinamicArray = new DynamicArray <int>(array);
                Console.WriteLine($"Dinamic array:\r\n {dinamicArray}, Length = {dinamicArray.Length}, Capacity = {dinamicArray.Capacity}");

                dinamicArray.Add(10);
                Console.WriteLine($"Dinamic array after add element with value 10:\r\n {dinamicArray}, Length = {dinamicArray.Length}, Capacity = {dinamicArray.Capacity}");

                int[] arrayForAdd = { 2, 3, 4, 7, 5, 8, 4, 1, 4, 6 };
                dinamicArray.AddRange(arrayForAdd);
                Console.WriteLine($"Dinamic array after add range:\r\n {dinamicArray}\r\nLength = {dinamicArray.Length}");

                Console.WriteLine($"Remove element with value 4: {dinamicArray.Remove(4)},\r\n Dinamic array: {dinamicArray}, Length = {dinamicArray.Length}, Capacity = {dinamicArray.Capacity}");

                dinamicArray.Insert(3, 333);
                Console.WriteLine($"Dinamic array after insert element 3 with value = 333:\r\n Dinamic array: {dinamicArray}, Length = {dinamicArray.Length}, Capacity = {dinamicArray.Capacity}");

                Console.WriteLine($"Array element with index = 2: {dinamicArray[2]}");

                List <int> enumerable = new List <int>();
                for (int i = 0; i < 10; i++)
                {
                    enumerable.Add(i + 1);
                }
                dinamicArray = new DynamicArray <int>(enumerable);
                Console.WriteLine($"Dinamic array:\r\n {dinamicArray}, Length = {dinamicArray.Length}, Capacity = {dinamicArray.Capacity}");

                var cur = dinamicArray.GetEnumerator();
                cur.MoveNext();
                Console.WriteLine(cur.Current);
                cur.MoveNext();
                Console.WriteLine(cur.Current);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            int a = 5;
            int b = 632;
            int c = -12;

            int[] arr = new int[] { 4, 8, 15, 16, 23, 42 };
            Console.WriteLine("This program contains dynamic arrays class. Here is an examples of using this class.\n");
            DynamicArray <int> dyn1 = new DynamicArray <int>();

            Console.WriteLine($"1. Empty array of type int has been created. Capacity of array: {dyn1.Capacity}:");
            Console.WriteLine($"2. Using method .Add to add our first element: {a}:");
            dyn1.Add(a);
            dyn1.ShowArray();
            Console.WriteLine($"3. Using method .Add to add another element: {b}:");
            dyn1.Add(b);
            dyn1.ShowArray();
            Console.WriteLine("4. Using method .AddRange to add array: { 4, 8, 15, 16, 23, 42 }:");
            dyn1.AddRange(arr);
            dyn1.ShowArray();
            Console.WriteLine($"5. Using method .Remove to remove second element ({b}):");
            dyn1.Remove(1);
            dyn1.ShowArray();
            Console.WriteLine($"6. Using method .Insert to insert element {b} at index {a}:");
            dyn1.Insert(b, a);
            dyn1.ShowArray();
            Console.WriteLine($"7. Using [index] to set value {c} for the last element:");
            dyn1[7] = c;
            dyn1.ShowArray();
            Console.WriteLine($"8. Using foreach (with own Enumerator) to get all vaqlues in one line:"); //foreach added
            foreach (var e in dyn1)
            {
                Console.Write(e + " ");
            }
            Console.WriteLine("\n\nEnd of program. Press any key.");
            Console.ReadKey();
        }
Exemple #4
0
        static void Main(string[] args)
        {
            var arr = new DynamicArray <int>();

            arr.AddRange(new int[] { 9, 8, 7, 12, 55, 77, 12, 34, 1 });
        }
Exemple #5
0
        private static void Main(string[] args)
        {
            var a = new DynamicArray <int>();

            a.Add(111);
            //Console.WriteLine(a[0]);
            int[] arr = new int[7] {
                1111111, 111111, 11111, 1111, 111, 11, 1
            };
            a.AddRange(arr);
            //for (int i = 0; i < a.Capacity; i++)
            //    Console.Write(a[i] + " ");
            //a.Remove(1);
            //Console.WriteLine();
            //for (int i = 0; i < a.Capacity; i++)
            //    Console.Write(a[i] + " ");
            //a.Insert(1, 1);
            //Console.WriteLine();

            a.Sort((t1, t2) =>
            {
                string s1 = t1.ToString();
                string s2 = t2.ToString();
                if (s1.Length == s2.Length)
                {
                    return(0);
                }
                else if (s1.Length > s2.Length)
                {
                    return(1);
                }
                else
                {
                    return(-1);
                }
            });
            for (int i = 0; i < a.Capacity; i++)
            {
                Console.Write(a[i] + " ");
            }
            a.FilterD((o) => o > 4);
            Console.WriteLine();
            for (int i = 0; i < a.Capacity; i++)
            {
                Console.Write(a[i] + " ");
            }
            Console.WriteLine();
            ///////////////////////////////////////////////////////////////////////////
            var d = new DynamicArray <double>();

            double[] arrD = new double[7] {
                8.1, 4.9, 6.05, 1.2, 14.4, 7.542, 0.22
            };
            d.AddRange(arrD);
            for (int i = 0; i < d.Capacity; i++)
            {
                Console.Write(d[i] + " ");
            }
            Console.WriteLine();
            d.Sort(DynamicArray <double> .Comparator);
            for (int i = 0; i < d.Capacity; i++)
            {
                Console.Write(d[i] + " ");
            }
            d.Filter((o) => o > 4.2);
            Console.WriteLine();
            for (int i = 0; i < d.Capacity; i++)
            {
                Console.Write(d[i] + " ");
            }
        }