ArrayList in C #

Source: Internet
Author: User

ArrayList are very similar to arrays, others call it an array list, ArrayList can be dynamically maintained

Tips:

Like arrays, the data stored in ArrayList is called an element, and the number of elements ArrayList can hold is ArrayList capacity, with a default initial capacity of 0, which can be indexed to the elements in the ArrayList, and the index starts at 0

ArrayList belongs to the System.Collections namespace and is a collection of

Using System.Collection

ArrayList arr=new ArrayList ();

ArrayList common methods and properties

Property name Description

Count gets the number of elements actually contained in the ArrayList

Return value type method name Description

The int add (object value) adds the object at the end of the ArrayList

void RemoveAt (int index) removes the element arraylist at the specified index

void remove (object value) removes a specific object from the ArrayList

void Clear () removes all elements from the ArrayList

Add data to ArrayList

1.ArrayList adding elements through the Add method

Return value: The value is an int type that is used to return the index of the element

Parameters: If the element added to ArrayList is a value type, the elements will be boxed into an object reference type and then saved, so all elements in ArrayList are references to objects

Accessing a single element in the ArrayList

ArrayList gets an element in the same way as the array, which is also accessed by index, the index of the first element in ArrayList is 0

Traversing elements in a ArrayList

Int[] Array=new int[]{1,2,3,4,5};

for (int i=0;i<array.length;i++) {

Console.WriteLine (Array[i]);
}

Delete an element in a ArrayList

ArrayList engineers=new ArrayList ();

Engineers. RemoveAt (0);

Enginneers. Remove (EMA);

MessageBox.Show (String. Format ("the department consists of {0} engineers", engineers. Count.tostring ()));

Se leave= (SE) engineers[0];

Messagebos.show (leave. Sayhi ());

The RemoveAt () and remove () methods can only delete one element

The clear () method clears the elements in the ArrayList

ArrayList in C #

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.