当前位置: 首页>>代码示例>>C#>>正文


C# GenericList.AddElementAtIndex方法代码示例

本文整理汇总了C#中GenericList.AddElementAtIndex方法的典型用法代码示例。如果您正苦于以下问题:C# GenericList.AddElementAtIndex方法的具体用法?C# GenericList.AddElementAtIndex怎么用?C# GenericList.AddElementAtIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在GenericList的用法示例。


在下文中一共展示了GenericList.AddElementAtIndex方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        static void Main(string[] args)
        {
            GenericList<int> listInt = new GenericList<int>();

            //Adding elements
            listInt.Add(99);
            listInt.Add(8);
            listInt.Add(0);
            listInt.Add(-7);
            listInt.Add(-9);
            Console.WriteLine(new string('=', 50));
            Console.WriteLine("Test Adding elements");
            Console.WriteLine("{0}", listInt);
            Console.WriteLine();

            //Inserting Elements
            listInt.AddElementAtIndex(2, -22);
            listInt.AddElementAtIndex(0, -21);
            Console.WriteLine(new string('=', 50));
            Console.WriteLine("Test Inserting elements");
            Console.WriteLine("{0}", listInt);
            Console.WriteLine();

            //Removing elements
            listInt.RemoveElementAtIndex(0);
            listInt.RemoveElementAtIndex(2);
            Console.WriteLine(new string('=', 50));
            Console.WriteLine("Test Removing elements");
            Console.WriteLine("{0}", listInt);
            Console.WriteLine();

            //Index of element by its value
            Console.WriteLine(new string('=', 50));
            Console.WriteLine("Test Index of elements");
            Console.WriteLine( "{0}", listInt.IndexOf(8));
            Console.WriteLine();

            //Min item
            Console.WriteLine(new string('=', 50));
            Console.WriteLine("Test finding min item");
            Console.WriteLine("{0}", listInt.Min<int>());
            Console.WriteLine();

            //Max item
            Console.WriteLine(new string('=', 50));
            Console.WriteLine("Test finding max item");
            Console.WriteLine("{0}", listInt.Max<int>());
            Console.WriteLine();
        }
开发者ID:bankoff,项目名称:TelerikAcademy,代码行数:49,代码来源:TestGenericList.cs

示例2: Main

    static void Main(string[] args)
    {
        // create an instance of the GenericList class;
        GenericList<int> list = new GenericList<int>();

        // add elements to the List using the method AddElement in GenericList class
        list.AddElement(1);
        list.AddElement(5);
        list.AddElement(10);
        list.AddElement(15);
        list.AddElement(20);

        //print the List - usint the overriden ToString() in GenericList class
        Console.WriteLine("List elements are: ");
        Console.WriteLine(list);

        // access the element by given index using the method AccessElementAtPosition in GenericList class
        int index = 2;
        Console.Write("Element at index {0} is: ", index);
        Console.WriteLine(list.AccessElementAtPosition(index));
        Console.WriteLine();

        // insert an element at given position
        index = 0;
        int num = 1000000000;
        list[index] = num;
        Console.WriteLine("Assign {0} to index {1}", num, index);
        Console.WriteLine(list);

        // add new element at given index
        //all elements after the given index are replaced to the right
        // the length of the array is increased by one
        index = 2;
        num = 22222;
        list.AddElementAtIndex(num, index);
        Console.WriteLine("Add new element with value {0} at index {1}", num, index);
        Console.WriteLine("Note that all element after the given index are replaced to the right \nand the length of the array is increased by one.");
        Console.WriteLine(list);

        // remove the element at given index
        // all elements after the given index are replaced to the left
        // the length of the array is decreased by one
        index = 3;
        Console.WriteLine("Remove the element at index {0}", index);
        Console.WriteLine("Note that all element after the given index are replaced to the left \nand the length of the array is decreased by one.");
        list.RemoveElementAtIndex(3);
        Console.WriteLine(list);

        // find element by its value using the method IndexOfElement in GenericList
        int elementValue = 5;
        Console.Write("The index of element with value {0} is: ", elementValue);
        Console.WriteLine(list.IndexOfElement(elementValue));

        // gets the Minimal element of the list using the GetMin() method in the GenericList class
        Console.Write("Minimal element of the array is: ");
        Console.WriteLine(list.GetMin());
        // gets the Maximal element of the list using the GetMax() method in the GenericList class
        Console.Write("Maximal element of the array is: ");
        Console.WriteLine(list.GetMax());

        // clear the list and assing the default value of T type to each index
        Console.WriteLine("Clear the list");
        list.ClearAllElements();
        Console.WriteLine(list);
    }
开发者ID:sabrie,项目名称:TelerikAcademy,代码行数:65,代码来源:GenericListTest.cs


注:本文中的GenericList.AddElementAtIndex方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。