Common methods of C # dynamic arrays

Source: Internet
Author: User
C # Arrays have a lot to learn, here we mainly introduce C # dynamic arrays, including the introduction of C # Dynamic array of common properties and so on.

C # Dynamic array objects are more complex arrays. We can see it as an expanded array of functions, but ArrayList is not the same as an array, what are the following functions and differences compared to arrays? Let's take a look at the specific content:

1. The capacity of the array is fixed, but the capacity of the ArrayList can be automatically expanded as needed. When we modify the capacity of the ArrayList, it is possible to automate memory reallocation and element copying, such as inserting n elements into the 1th index bit, and after inserting, the index of the element is sequentially n-position, which is the dynamic version of the array type.

2.ArrayList provides a way to add, insert, or remove a range of elements. However, in an array, you can only get or set the value of an element at a time, such as by using an index.

3.ArrayList has only one dimension, and the array can be multidimensional.

How do I declare a C # dynamic array?

ArrayList al=new ArrayList (capacity);//initial capacity capacity also can not write

The reason is that even if you do not identify the initial capacity, capacity is not enough, will automatically expand by multiple.

Common Properties for C # dynamic arrays
Capacity: Gets or sets the number of elements that ArrayList can contain.
Count: Gets the number of elements actually contained in the ArrayList.
IsReadOnly: Gets a value that indicates whether ArrayList is read-only.
Item: Gets or sets the element at the specified index.

Common methods of C # dynamic arrays
add element-arraylist.add (value), add element value with Add method, we can index add element arraylist[index]=value;
Inserts an element-arraylist.insert (Index,value), and inserts the value of the element into the Index position.
Delete element-arraylist.clear (); Remove all elements from the collection
Arraylist.remove (value); Delete element by collection element value
Arraylist.removeat (Index): Deletes an element according to the element index of the collection
Reduced capacity-arraylist.trimtosize (); Reduces the capacity of the collection to the size of the actual number of elements

After performing the delete operation, you should develop a good habit of reducing capacity, save memory space and improve performance.

Find element-In addition to searching by an index of an array, you can also use Arraylist.contains (value), find the collection by element value, and return True if included, False if not included.

The basics of C # Dynamic arrays are introduced here, and hopefully it will help you learn and learn about C # dynamic arrays.


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.