C # ArrayList usage

Source: Internet
Author: User

The System. Collections. ArrayList class is a special array. By adding and deleting elements, You can dynamically change the length of the array.

I. Advantages

1. Supports automatic resizing.

2. Flexible element insertion

3. Flexible deletion of Elements

4. flexible access to elements

Ii. Limitations

Compared with the average array, the speed is lower.

3. Add Elements

1. public virtual int Add (object value );

Add the object to the end of ArrayList

ArrayList aList = new ArrayList ();

AList. Add ("");

AList. Add ("B ");

AList. Add ("c ");

AList. Add ("d ");

AList. Add ("e ");

Content is abcde

2. public virtual void Insert (int index, object value );

Insert the element to the specified index of ArrayList

ArrayList aList = new ArrayList ();

AList. Add ("");

AList. Add ("B ");

AList. Add ("c ");

AList. Add ("d ");

AList. Add ("e ");

AList. Insert (0, "aa ");

The result is aaabcde.

3. public virtual void InsertRange (int index, ICollectionc );

Insert an element in the set to the specified index of ArrayList.

ArrayList aList = new ArrayList ();

AList. Add ("");

AList. Add ("B ");

AList. Add ("c ");

AList. Add ("d ");

AList. Add ("e ");

ArrayList list2 = new ArrayList ();

List2.Add ("tt ");

List2.Add ("ttt ");

AList. InsertRange (2, list2 );

The result is abtttttcde.

Iv. Delete

A) public virtual void Remove (object obj );

Remove the first matching item of a specific object from the ArrayList. Note that it is the first

ArrayList aList = new ArrayList ();

AList. Add ("");

AList. Add ("B ");

AList. Add ("c ");

AList. Add ("d ");

AList. Add ("e ");

AList. Remove ("");

The result is bcde.

2. public virtual void RemoveAt (intindex );

Removes the element at the specified index of ArrayList.

AList. Add ("");

AList. Add ("B ");

AList. Add ("c ");

AList. Add ("d ");

AList. Add ("e ");

AList. RemoveAt (0 );

The result is bcde.

3. public virtual void RemoveRange (int index, int count );

Remove a certain range of elements from the ArrayList. Index indicates the Index, and count indicates the number starting from the Index.

AList. Add ("");

AList. Add ("B ");

AList. Add ("c ");

AList. Add ("d ");

AList. Add ("e ");

AList. RemoveRange (1, 3 );

The result is AE.

4. public virtual void Clear ();

Remove all elements from ArrayList.

5. Sorting

A) public virtual void Sort ();

Sorts the elements in an ArrayList or a part of it.

ArrayListaList = newArrayList ();

AList. Add ("e ");

AList. Add ("");

AList. Add ("B ");

AList. Add ("c ");

AList. Add ("d ");

DropDownList1.DataSource = aList; // DropDown ListDropDownList1;

DropDownList1.DataBind ();

The result is eabcd.

ArrayList aList = new ArrayList ();

AList. Add ("");

AList. Add ("B ");

AList. Add ("c ");

AList. Add ("d ");

AList. Add ("e ");

AList. Sort (); // Sort

DropDownList1.DataSource = aList; // DropDownListDropDownList1;

DropDownList1.DataBind ();

The result is abcde.

B) public virtual void Reverse ();

Reverses the order of elements in an ArrayList or a part of it.

ArrayList aList = new ArrayList ();

AList. Add ("");

AList. Add ("B ");

AList. Add ("c ");

AList. Add ("d ");

AList. Add ("e ");

AList. Reverse (); // Reverse

DropDownList1.DataSource = aList; // DropDownListDropDownList1;

DropDownList1.DataBind ();

The result is edcba.

6. Search

A) public virtual int IndexOf (object );

B) public virtual int IndexOf (object, int );

C) public virtual int IndexOf (object, int, int );

Returns the index starting from scratch for the first matching item of a value in the ArrayList or part of it. -1 is not found.

ArrayList aList = new ArrayList ();

AList. Add ("");

AList. Add ("B ");

AList. Add ("c ");

AList. Add ("d ");

AList. Add ("e ");

IntnIndex = aList. IndexOf ("a"); // 1

NIndex = aList. IndexOf ("p"); // not found,-1

D) public virtual int LastIndexOf (object );

E) public virtual int LastIndexOf (object, int );

F) public virtual int LastIndexOf (object, int, int );

Returns the index starting from scratch for the last matching item of a value in the ArrayList or part of it.

ArrayList aList = new ArrayList ();

AList. Add ("");

AList. Add ("B ");

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.