C # basic knowledge-array _ ArrayList_List (9 ),

Source: Internet
Author: User

C # basic knowledge-array _ ArrayList_List (9 ),

The previous two documents described the object-oriented concept in C #. In fact, this concept is common in many languages, but the specific implementation process of each language is different, for example, Java, C ++, python, etc. These are popular object-oriented languages which belong to the top few in the programming language ranking. What is hard to understand in Object-Oriented is the concept of polymorphism. I will leave polymorphism to the next chapter. Now, I will continue to learn basic syntaxes and understand the meaning to better understand them.

 

Array
An array, as its name implies, is a set of similar data elements. An array is not only a set of numbers but may be of multiple types. However, elements in an array must be of the same data type, so what are elements of similar data? In the first article, we will introduce the most common data types in C #15 (the types can be expanded by themselves). Each type has a maximum value and a minimum value, and each value is equivalent to an element, if an array of the int type is input, only elements of the int type can be stored. If the string type is stored in the int type, the compiler may report an error, arrays are the first to appear in C # And are stored continuously in the memory. Therefore, the index speed is very fast. Next let's take a look at how to declare an array.

 

Int type is used here:

1 // create an array of 3 2 3 int [] intArray = new int [3]; 4 5 // assign a value of 6 7 intArray [0] = 1; 8 9 intArray [1] = 2; 10 11 intArray [2] = 3; 12 13 // modify 14 15 intArray [2] = 4;

 

However, there are some shortcomings in the array. For example, it is very troublesome to insert data between two data arrays. All elements after the inserted array must be backward, in addition, the length of the array must be specified when the array is declared. The length of the array is too long, which may cause a waste of memory. Excessive length may also cause data overflow errors. To address these disadvantages of the array, arrayList objects are introduced to overcome these shortcomings.

 

ArrayList

In the namespaceSystem. CollectionThe object elements stored in ArrayLis are dynamically allocated and scaled up and down as needed. Therefore, you do not need to specify the length of an ArrayLis object when declaring it.

1 // declare an ArrayList object 2 ArrayList arrayList = new ArrayList (); 3 4 // Add data 5 arrayList. add (123); 6 arrayList. add ("Leon"); 7 8 // modify data 9 arrayList [1] = 32; 10 11 // delete 12 arrayList. remove (0); 13 14 // insert data 15 arrayList. insert (1, 0,445 );

 

In the above example, the ArrayList seems to solve the problem left over from the above array. At that time, when you compile it, you will encounter an exception, if any students who input code compilation on the compiler find that arrayList. the Add () method accepts the type of an object. When I assign a value to this position in arrayList [0], the performance loss caused by a packing box is displayed, in addition, when I use Add for the second time, a variable of the string type is inserted, so there are two different data types in the same array, and they are normal during compilation, therefore, the type cannot be checked during compilation, and an error occurs during running. Packing and unpacking is actually the performance loss caused by data conversion between the object type and other types. The specific process can be found online.

 

Generic List

Because ArrayList has the disadvantages of unsafe type and packing and unpacking, Microsoft has introduced generic type. The List class and ArrayList class have similar usage, but the most important thing is that the data object type must be specified when the List declares the set.

1 // Declare List set object 2 List <string> listString = new List <string> (); 3 4 // Add 5 listString. add ("leon"); 6 7 // modify 8 listString [0] = "newLeon"; 9 10 // delete data 11 listString. removeAt (0 );

 

Here we can see that the usage of List and ArrayList is similar, the difference is that the type of data object must be specified when the List is declared, in this way, if you try to insert an int type value into listString, the compiler will report an error. during compilation, you can avoid a type security problem and the performance loss caused by packing and unpacking.

 

Summary:

The storage capacity of the array is fixed, and only one value can be operated at a time. It is troublesome and inconvenient to insert an element between two values.

Although a set of methods defined in ArrayList can be used to conveniently operate arrays and delete and insert elements, many resources are wasted due to the binning process, in addition, no security type check is provided during compilation, so that other types such as string can be inserted in an array with int type, although no error is reported during compilation, however, an exception occurs during running.

Generic List: the generic int type is strongly typed. during compilation, the type will be checked and the loss caused by packing and unpacking will be reduced.

Note that arrays can be multidimensional. However, ArrayList and List always have only one dimension.

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.