(Teaching ideas C # Collection I) Overview of the collection, dynamic array ArrayList

Source: Internet
Author: User

In this section we are going to learn about collections, what are collections? The collection is like an array, used to store and manage a set of specific types of data objects, in addition to the basic data processing functions, the collection directly provides a variety of data structure and algorithm implementation, such as queues, linked lists, sorting and so on, you can easily complete complex data operations. When using arrays and collections, you first join the System.Collections namespace, which provides interfaces and classes that support various types of collections. The collection itself is also a type that can basically be used as a container for storing a set of data objects, and because of the object-oriented nature of C #, the collection of managed data objects is also implemented as objects, while the data objects stored in the collection are called collection elements. The concept of interface is mentioned here, it is also an important standard of object-oriented programming evolution, we do not do too much to explain here, first focus on learning the objects in the collection and their use of it, let's learn the first collection:

The dynamic array Arraylist.arraylist class provides an inherited IList interface. What is inheritance? This is also one of the important features of object-oriented language, now you first understand that if an object inherits a class or interface, it also has the methods and attributes in the class and interface, and you can use these inherited methods and properties to do the appropriate action, such as: the array addition element does not have the Add () method. But dynamic array ArrayList inherits an interface that adds an element with the Add () method, and when it wants to add elements, it can be indexed or inherited from the Add () method. With the further study, I will give you a detailed explanation of the concept of inheritance and the benefits of using inheritance. So let's take a look at this interface inherited by a dynamic array IList what does it feature?

IList interface: Defines the method that uses index to access collection object, also inherits ICollection and IEnumerable interface, besides implementing the original method member of interface, it also defines several special method members, such as new, removed, Inserts an element at the specified location or returns the index of the location of a particular element in the collection, which provides an array-like element access feature for the collection object.

Ilsit Interface Members: Add, insert, RemoveAt, Remove, contains, clear, IndexOf method, its biggest feature is to provide a similar array index access mechanism.

The ArrayList object is a more complex array. We can look at it as an array of extended functions, but ArrayList is not the same as an array, and the following features and differences are:

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, we can automatically do memory reallocation and element replication, such as inserting n elements into the 1th index bit, and after inserting, the index of the element is arranged backwards n positions, 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 once, such as by using an index assignment.

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

4.ArrayList can hold data of any data type, while an array can store data of the same data type only.

This mechanism that can hold any data type, which we call boxing, I'll explain in the second half of this lesson that now all you need to remember is that no matter what data type you add to the dynamic array, it will be converted to the object data type, so when you traverse ArrayList, To define a variable of type object to receive the value of each item that traverses it.

How do I declare a dynamic array?

ArrayList al=new ArrayList (Capacity);//initial capacity Capacity is not writable either.

The reason is that even if the initial identification of capacity, capacity is not enough, will automatically expand by multiple.

Let's take a look at the common properties of the dynamic array

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 the ArrayList is read-only.

Item Gets or sets the element at the specified index.

Common methods of dynamic arrays

Increase element-al.add (value), add element value by adding method

modifying element-al[index]=value, using indexes to modify the value of an element

Inserts the element-al.insert (Index,value), inserts the value of the element into the Index position.

Delete Element-al.clear (), delete all elements in the collection

AL. Remove (value); Delete element by collection element value

AL. RemoveAt (index), deleting elements by collection's element index

Reduced capacity-al.trimtosize (); Reduces the capacity of the collection to the size of the actual number of elements

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.