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

The Add method is basically used in every program using ArrayList. 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, compared with clearing all the elements of the original ArrayList object, directly creating a new ArrayList object may make the Code 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.

 

Author: Korean lock Source: http://www.cnblogs.com/hans_gis/

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.