C # Learning ArrayList

Source: Internet
Author: User

C # Learning ArrayList

In the process of learning C #, ArrayList is actually a dynamic array that can add, insert, and delete elements.

Operations such as clearing, sorting, and reversing can implement the ICollection and IList interfaces and flexibly set the array size. In this case, how does ArrayList expand and what methods does it have? Next we will introduce it.

First, you must manually add a reference namespace System. Collection before using ArrayList.

The following format is

ArrayList List = new ArrayList (); // create a new object and keep all attributes of ArrayList.

For (int I = 0; I <10, I ++) // Add 10 Int elements to the array.

The next step is how to process some ArrayList methods.

List. add (I); // use add to Add an array. In this case, you can use "subscript" (INDEX) to access data. The data must be of the object type and should be converted if necessary, lee's conversion principle must be met

List. RemoveAt (I); // remove element I

List. Insert (I, I + 1); // Insert a value to a position under the target

List. InsertRange (4, nums); // insert an array to an index position (that is, add a batch of elements from the specified position, and move the elements after the List in turn)

List. RemoveAt (I); // remove the value corresponding to the index

List. RemoveRange (3,100); // Number of removed indexes starting from the specified index. If the index is exceeded, an exception is reported.

List. Contains (I); // determines whether the collection Contains this element.

Int number = List. IndexOf (I); // you can specify the index corresponding to an element.

Int [] nums = [1, 2, 3];

List. Remove (I); // removes an element.

List. Reverse (); // Reverse

ArrayList. Sort (); // sorting, which can be Max, Min, Avg, etc.

Int num = List. Count; // number of elements in the Set

List. clear // clear all current elements

The above are some common methods of ArrayList, which should be used flexibly during use.

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.