C # common methods of dynamic array ArrayList,

Source: Internet
Author: User

C # common methods of dynamic array ArrayList,

Dynamic Array

Dynamic addition and reduction of elements enable the ICollection and List and IEnumerable interfaces to flexibly set insecure array size Collection types. When the elements are value-type, the timeliness rate is not high (resulting in inefficiency due to packing and unpacking ). high)

Common ArrayList Methods

//to create an ArrayListArrayList arrayList = new ArrayList();//to add valuesarrayList.Add(32);arrayList.Add("Puma");arrayList.Add('a');//ArrayList can have different kinds of type of value//to outputforeach (object obj in arrayList){Console.Write(obj + " ");}Console.WriteLine();//show the capacityConsole.WriteLine("the Capacity is:" + arrayList.Capacity);Console.WriteLine("Adding one more element");arrayList.Add(5);Console.WriteLine("Now the Capacity is:" + arrayList.Capacity);//count: gets the number of elements actually contained in ArrayListConsole.WriteLine("The elements in the ArrayList are:" + arrayList.Count);//contains : Determines whether an element is in the ArrayListif(arrayList.Contains(32))Console.WriteLine("It exists!");elseConsole.WriteLine("It doesn'n exist");//Insert: Insert an element into ArrayList at the specified indexarrayList.Insert(2,"real");foreach (object obj in arrayList){Console.Write(obj + " ");}Console.WriteLine();//IndexOf:searches for the specified object and returns the zero-based index of the first occurrence within the entire ArrayList.Console.WriteLine(arrayList.IndexOf(32));//Remove:Removes the first occurrence of a specific object from the ArrayListarrayList.Remove(32);foreach (object obj in arrayList){Console.Write(obj + " ");}Console.WriteLine();//Reverse:Reverses the order of the elements in the entire ArrayListarrayList.Reverse();foreach (object obj in arrayList){Console.Write(obj + " ");}Console.WriteLine();//Sort:Sorts the elements in the entire ArrayList.arrayList.Add(3);arrayList.Add(42);arrayList.Remove("Puma");arrayList.Remove("real");arrayList.Remove('a');//before sortingforeach (object obj in arrayList){Console.Write(obj + " ");}Console.WriteLine();arrayList.Sort();//after sortingforeach (object obj in arrayList){Console.Write(obj + " ");}Console.WriteLine();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.