System. Collections. 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. You can flexibly Delete elements.

Ii. Limitations

Compared with the average array, the speed is lower.

3. Add Elements

1. publicvirtualintadd (objectvalue );

Add the object to the end of arraylist

Arraylistalist = newarraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e ");
Content is ABCDE

2. publicvirtualvoidinsert (intindex, objectvalue );

Insert the element to the specified index of arraylist

Arraylistalist = newarraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e ");
Alist. insert (0, "AA ");

The result is aaabcde.

3. publicvirtualvoidinsertrange (intindex, icollectionc );

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

Arraylistalist = newarraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e ");
Arraylistlist2 = newarraylist ();
List2.add ("TT ");
List2.add ("TTT ");
Alist. insertrange (2, list2 );

The result is abtttttcde.

Iv. Delete

A) publicvirtualvoidremove (objectobj );

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

Arraylistalist = newarraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e ");
Alist. Remove ("");

The result is bcde.

2. publicvirtualvoidremoveat (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. publicvirtualvoidremoverange (intindex, intcount );

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. publicvirtualvoidclear ();

Remove all elements from arraylist.

5. Sorting

A) publicvirtualvoidsort ();

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; // dropdownlistdropdownlist1;
Dropdownlist1.databind ();

The result is eabcd.

Arraylistalist = newarraylist ();
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) publicvirtualvoidreverse ();

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

Arraylistalist = newarraylist ();
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) publicvirtualintindexof (object );
B) publicvirtualintindexof (object, INT );
C) publicvirtualintindexof (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.

Arraylistalist = newarraylist ();
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) publicvirtualintlastindexof (object );
E) publicvirtualintlastindexof (object, INT );
F) publicvirtualintlastindexof (object, Int, INT );

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

Arraylistalist = newarraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("A"); // same as 0
Alist. Add ("D ");
Alist. Add ("e ");
Intnindex = alist. lastindexof ("A"); // The value is 2 instead of 0.

G) publicvirtualboolcontains (objectitem );

Determines whether an element is in the arraylist. True is returned for inclusion; otherwise, false is returned.

VII. Miscellaneous

1. publicvirtualintcapacity {Get; set ;}

Gets or sets the number of elements that an arraylist can contain.

2. publicvirtualintcount {Get ;}

Obtains the number of actually contained elements in the arraylist.
Capacity is the number of elements that can be stored in arraylist. Count is the number of actually contained elements in the arraylist. Capacity is always greater than or equal to count. If the Count value exceeds the capacity when an element is added, the capacity of the List is doubled by automatically allocating an internal array.
If the capacity value is explicitly set, the internal array also needs to be reassigned to accommodate the specified capacity. If capacity is explicitly set to 0, the common language runtime sets it to the default capacity. The default capacity is 16.
After clear is called, count is 0, and capacity switches to the default capacity of 16 instead of 0.

3. publicvirtualvoidtrimtosize ();

Set the capacity to the actual number of elements in the arraylist.
If new elements are not added to the list, this method can be used to minimize the memory system overhead of the list.
To completely clear all elements in the list, call the clear method before calling trimtosize. If this parameter is left blank, arraylist sets the capacity of arraylist to the default capacity instead of zero.

Arraylistalist = newarraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e"); // COUNT = 5, capacity = 16,
Alist. trimtosize (); // COUNT = capacity = 5;

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.