Dynamic Array arraylist in C #

Source: Internet
Author: User

Arraylist is a dynamic array that implements the ilist, icollection, ienumerable, and icloneable interfaces. Even if. netframwork provides some more durable and popular new classes, arraylist is still very useful. The following are some examples of using arraylist in C #. Let's take a look at its basic usage and some advanced usage.

Add Element

EachProgramThe add method is basically used. This method appends an element object to the end of arraylist. When the memory permits, You can continuously add elements to this set, and their elements are stored in heap format. See the following example.

 

The storage of elements in the arraylist class is of the object type, which means that the arraylist object can accept any value in. Net in principle and usually generate packing and unpacking when storing or retrieving the value type. See the following example.

Add an arraylist to another arraylist

There are several different ways to add an arraylist to another arraylist, but the best is to use the addrange method. In the. NET Framework, addrange uses the array. Copy or copyto method to achieve better performance than some loops. The following is an example.

The Count attribute and the clear method of arraylist

The arraylist class provides a virtual attribute -- count. When the Count attribute is used, instead of calculating the number of elements in the arraylist object, a cached field value is directly returned, this means that the number of elements obtained using this attribute is very fast. Therefore, the Count attribute is directly obtained in the program for use, instead of being stored in another variable.

The clear method of the arraylist class is used to remove all its elements. In the. NET Framework, it calls the array. Clear method. In some cases, you can directly create a new arraylist object compared to clearing all the elements of the original arraylist object,CodeIt may look more neat.

Arraylist's sort and reserve methods

You can use the sort method to sort arraylist or some of its elements. The default value is ascending. As mentioned above, different types of values can be accepted in arraylist. However, to use the sort method, you must ensure that the elements to be sorted are of the same data type. In other words, when arraylist has different types of elements, you can still use the sort method to sort some of the elements, but you must specify the range of these elements.

Similar to sort, the reserve method is used to invert the order of arraylist or some of its elements. The Processing Method for different types of elements in arraylist is the same as that of sort.

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.