Detailed description of graphic code describing data structure 3:arraylist in C #

Source: Internet
Author: User


In the previous section, when we introduced the data structure of array, we said that it was static, the number of elements per dimension was compiled, and it was uniquely determined, and its type was strongly typed.



So, in this section, we'll talk about another data structure similar to array but overcomes the disadvantage of array:ArrayList.
First of all, it is not static, at compile time the number of elements of each dimension is not specified, the system default element number is 16, when the element increases and is about to be greater than 16 o'clock, it will increase the expansion to 32, in order to increase the regular growth, change the hours, the opposite processing.
Second, the element type is a weak type, object. At run time, depending on the type that is actually given, the type of each element is determined, that is, the elements in this set can be a variety of different elements, mixed-style.
Look first. NET provides the ArrayList interface:



1) object creation and initialization

// Object creation
            ArrayList arrayList = new ArrayList () {3.14, "vuefine"};
            // Add elements
            arrayList.Add ("Hello wolrd");
            arrayList.Add (5);
2) Access element

            // Access element
            object ele0 = arrayList [0];
            Type t0 = ele0.GetType (); // double
3) Modify elements

             // Modify the element
            arrayList [0] = "Ni hao";
4) Delete elements

             // Delete element
            // Remove existing objects
            arrayList.Remove (5);
            arrayList.RemoveAt (0); // Remove non-existent objects
            arrayList.Remove (12); // Do not throw exception
5) ArrayList and other object relationships

            object cloneAL = arrayList.Clone (); // Create a shallow copy
            Type tClone = cloneAL.GetType (); // Array is abstract, array can only be created by static method
            Array array = Array.CreateInstance (typeof (object), arrayList.Count);
            arrayList.CopyTo (array); // Copy to array
related articles:

js implements ArrayList function with example code

PHP implementation of C # cottage ArrayList

ArrayList sample code analysis of Java collection

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.