C # data structure and algorithm Learning Series 3-features and applications of the arraylist class

Source: Internet
Author: User

 When you cannot know the size of the array in advance or the size of the array may change during the program running, static arrays are not very suitable. One solution to this type of problem is to use the array type that can automatically adjust its own size when the array exceeds the storage space. This array is called arraylist. It is the content of the system. Collections namespace in the. NET Framework library. The arraylist object has the capacity attribute that can store the array size. The initial value of this attribute is 16. Capacity
The attribute will add 16 additional elements to the arraylist bucket. The use of arraylist is more effective than the use of redim preserver with a standard array when the number of elements in the array may be expanded or reduced.

The arraylist class contains several methods and attributes used for arraylist. The following lists some of the most common methods and attributes:

1. Add (): Add an element to the arraylist and return the location of the element.
2. addrange (): add the cluster element at the end of the arraylist.
3. Capacity: number of elements that can be included in the arraylist.
4. Clear (): Remove all elements from arraylist.
5. Contains (): determines whether the specified object is in the arraylist.
6. Copy to (): copy an arraylist or a segment of it to an array.
7. Count: returns the number of current elements in the arraylist.
8. getenumerator (): returns the counter of the iterative arraylist.
9. getrange (): returns the subset of arraylist as the arraylist.
10. indexof (): returns the index that appears for the first time of the specified data item.
11. insert (): insert an element at the specified index of arraylist.
12. insertrange (): inserts the cluster element from the index specified by arraylist.
13. Item (): gets or sets an element at the specified index.
14. Remove (): removes the first appearance of a specified data item.
15. removeat (): removes an element from the specified index.
16. Reverse (): reverse the order of elements in the arraylist.
17. Sort (): sorts the elements in the arraylist in the Arabic alphabetic order.
18. toarray (): copy the elements of arraylist to an array.
19. trimtosize (): Set the arraylist capacity for the number of elements in arraylist.

The commonly used methods of the arraylist class are basically here, and they are quite easy to 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.