Esempio n. 1
0
        public static void Main()
        {
            GenericList <int> list = new GenericList <int>();

            list.Add(1);
            list.Add(2);
            list.Add(3);
            list.Add(4);
            list.Add(5);
            Console.WriteLine("Making a new list:\n " + list);

            Console.WriteLine("Inserting a number 6 at position 2!");
            list.Insert(2, 6);
            Console.WriteLine(list);

            Console.WriteLine("The position of number 3 is {0}!", list.IndexOf(3));

            Console.WriteLine("Remove an element on position 3.");
            list.RemoveAt(3);
            Console.WriteLine(list);

            Console.WriteLine("The MIN element in the list is {0}", list.Min());
            Console.WriteLine("The MAX element in the list is {0}", list.Max());

            list.Clear();

            Console.WriteLine("The list after list.Clear()! \n" + list);

            //Console.WriteLine(list.Min());
        }
        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);
        }
        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. 4
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);
            }
        }