Esempio n. 1
0
File: Test.cs Progetto: HD-LB/OOP
        static void Main()
        {
            var list = new GenericList <int>(5);

            list.Add(5);
            list.Add(10);
            list.Add(20);


            list.Insert(1, 123);

            list.Add(0);
            list.Remove(4);

            Console.WriteLine(list);

            Console.WriteLine(list.Min());
            Console.WriteLine(list.Max());
        }
        public static void Main()
        {
            GenericList <int> myList = new GenericList <int>(6);

            // Adding elements
            for (int i = 0; i < 10; i++)
            {
                myList.Add(i);
            }

            //Select element by index
            int sellectedElement = myList[4];

            myList[5] = 55;
            // Uncomment next line for Out of Range exeption
            //testList[100] = 100;

            // Remove element at given position
            myList.RemoveAt(4);

            // Insert elemetn at a given position
            myList.InsertAt(22, 4);

            //Clear the collection
            //myList.Clear();



            //Find first occurence by given a searched element

            // Vlaue type chack
            GenericList <int> myInts = new GenericList <int>(6);

            for (int i = 11; i < 11 + 6; i++)
            {
                myInts.Add(i);
            }
            int foundIx = myInts.FindFirst(13);

            // Reference type check
            Point3D A = new Point3D(1, 2, 3);
            Point3D B = new Point3D(2, 1, 3);
            Point3D C = new Point3D(3, 2, 1);

            GenericList <Point3D> myPoints = new GenericList <Point3D>(6);

            myPoints.Add(A);
            myPoints.Add(B);
            myPoints.Add(C);

            Point3D searchedPoint = new Point3D(2, 1, 3);

            foundIx = myPoints.FindFirst(searchedPoint);
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var numbers = new GenericList <int>();

            for (int i = 2; i <= 4; i++)
            {
                numbers.Add(i);
            }

            //numbers.Print();
            //for (int i = 5; i <= 7; i++)
            //{
            //    numbers.RemoveFirst(i);
            //}
            //numbers.Print();

            //Console.WriteLine(numbers[0]);

            //numbers[1] = 33;
            //numbers[3] = 99;
            //numbers[5] = -1;
            //numbers.Print();

            //numbers.RemoveAt(1);
            //numbers.RemoveAt(1);

            //numbers.Print();

            //numbers.InsertAt(33, 1);
            //numbers.Print();
            //numbers.Clear();
            //numbers.Print();

            //numbers--;
            //--numbers;
            //numbers.Print();

            if (numbers)
            {
                Console.WriteLine(numbers[0] + " " + numbers[numbers.Count - 1]);
            }

            var minV = numbers.Min <Int32>();

            Console.WriteLine(minV);

            var maxV = numbers.Max <Int32>();

            Console.WriteLine(maxV);

            //Console.WriteLine(numbers[10]);
        }
Esempio n. 4
0
        static void Main()
        {
            var test = new GenericList <int>();

            for (int i = 0; i < 11; i++)
            {
                test.Add(i);
            }
            Console.WriteLine(test.ToString());

            test.AddAtIndex(4, 56);

            Console.WriteLine(test.ToString());

            Console.WriteLine(test.Max());   // Testing Min and Max methods Task 7
            Console.WriteLine(test.Min());
        }
Esempio n. 5
0
        public static void Main()
        {
            var myListInt = new GenericList <int>();

            myListInt.Add(2);
            myListInt.Add(5);
            myListInt.Add(16);
            myListInt.Add(78);
            myListInt.Add(52);
            myListInt.Add(9);

            Console.WriteLine("Min element in list is: {0}", myListInt.Min());
            Console.WriteLine();
            Console.WriteLine("Max element in list is: {0}", myListInt.Max());
            Console.WriteLine();

            var myListString = new GenericList <string>();

            myListString.Add("Ivan");
            myListString.Add("Petar");
            myListString.Add("Georgi");
            myListString.Add("Todor");
            myListString.Add("Stoyan");

            Console.WriteLine(myListString[4]);
            Console.WriteLine();

            Console.WriteLine(myListString.ToString());
            Console.WriteLine();

            myListString[4] = "Soyancho";

            myListString.Insert(3, "Angel");

            Console.WriteLine(myListString.ToString());

            Console.WriteLine();

            myListString.Delete(1);

            Console.WriteLine(myListString.ToString());
            Console.WriteLine();

            myListString.Clear();

            Console.WriteLine(myListString.ToString());
            Console.WriteLine();
        }
        static void Main()
        {
            GenericList <int> list = new GenericList <int>(8);

            //GenericList<char> list = new GenericList<char>(8);

            Console.WriteLine("Initial list capacity: {0}\n", list.Capacity);

            for (int i = 0; i < 10; i++)
            {
                list.Add(i);
                //list.Add((char)('A' + i));
            }
            Console.WriteLine("List with 10 added elements and new capacity = {0}:", list.Capacity);
            Console.WriteLine(list + Environment.NewLine);

            list.InsertAt(4, 20);
            //list.InsertAt(4, 'Z');
            Console.WriteLine("List with an element insserted at position [4]:");
            Console.WriteLine(list + Environment.NewLine);

            list.RemoveAt(0);
            Console.WriteLine("List with an element removed from position [0]:");
            Console.WriteLine(list + Environment.NewLine);

            int max = list.Max();
            int min = list.Min();
            //char max = list.Max();
            //char min = list.Min();
            int indexOfMax = list.IndexOf(max);
            int indexOfMin = list.IndexOf(min);

            Console.WriteLine("Index of max = {0} is: {1}", max, indexOfMax);
            Console.WriteLine("Index of min = {0} is: {1}", min, indexOfMin);
            Console.WriteLine();

            list.Clear();
            Console.WriteLine("Cleared list:");
            Console.WriteLine(list);
        }
Esempio n. 7
0
        public void AddAtIndex(int index, T newElement)
        {
            if (index >= this.Capacity || index < 0)
            {
                throw new IndexOutOfRangeException(String.Format(
                                                       "Invalid index: {0}.", index));
            }

            var temp = new GenericList <T>();

            for (int i = index; i < this.Count; i++)
            {
                temp.Add(this.elements[i]);
            }

            this.elements[index] = newElement;

            for (int i = 0, j = index + 1; i <= temp.Count; i++, j++)
            {
                this.elements[j] = temp.elements[i];
            }
            this.Count++;
        }
Esempio n. 8
0
        public static void Main()
        {
            var firstList = new GenericList <int>();

            for (int i = 0; i < 6; i++)
            {
                firstList.Add(i);
            }

            Console.WriteLine("Initialize GenericList");
            Console.WriteLine(firstList);

            Console.WriteLine("Add item at index 3");
            firstList.AddAtIndex(10, 3);
            Console.WriteLine(firstList);

            Console.WriteLine("Deleted item at index 3");
            firstList.DeleteAtIndex(3);
            Console.WriteLine(firstList);

            Console.WriteLine("Min item In the array is: {0}", firstList.Min());
            Console.WriteLine("Max item In the array is: {0}", firstList.Max());
        }
Esempio n. 9
0
        static void Main()
        {
            //Empty constructor with the default capacity. If some value is written like parameter in the constructor
            //the capacity will be equal to this parameter.
            GenericList <int> col = new GenericList <int>();

            //add some elements to the collection
            col.Add(2);
            col.Add(77);
            col.Add(33);
            col.Add(7447);
            Console.WriteLine("the elements of the collection: " + col.ToString());
            Console.WriteLine("The capacity = " + col.Capacity);
            Console.WriteLine("The element at index[1] is: " + col[1]);
            //set new value at index [1]
            col[1] = 666;
            Console.WriteLine("The new value in index [1] is: " + col[1]);

            Console.WriteLine("Count of the collection = " + col.Count);
            //Removing element at index [0]
            col.RemoveAt(0);
            Console.WriteLine();
            Console.WriteLine("Collection after removing: " + col.ToString());
            Console.WriteLine("Count of the collection after removing: " + col.Count);
            //inserting element in the collection
            Console.WriteLine("collection before inserting: " + col.ToString());
            col.InsertAt(2, 9999999);
            Console.WriteLine("collection after inserting: " + col.ToString());

            //Clear and print capacity and Count
            col.Clear();
            Console.WriteLine();
            Console.WriteLine("After the clear -----------------------");
            Console.WriteLine("Capacity = " + col.Capacity);
            Console.WriteLine("Count = " + col.Count);
            Console.WriteLine("Collection: " + col.ToString());
            Console.WriteLine();


            //finding index by its value if exist. If do not exist return -1
            col.Add(2);
            col.Add(3);
            col.Add(-1);
            col.Add(-99);
            //print the collection after adding some values
            Console.WriteLine("Collection: " + col.ToString());

            int max = col.Max();

            Console.WriteLine("max = " + max);
            int min = col.Min();

            Console.WriteLine("min = " + min);


            int value = 3;
            int index = col.indexOf(value);

            if (index > 0)
            {
                Console.WriteLine("The element with value: {0} is on index: {1}", value, index);
            }
            else
            {
                Console.WriteLine("The element with value: {0} is not found.", value);
            }
        }