The difference between arrays, lists, and ArrayList

Source: Internet
Author: User
Tags type casting

Some knowledge points may always be used, but in the actual development we may only know its why, so often the summary will be a great help to our improvement and progress, here to record their work outside the problem, continuous updating, welcome Master treatise.

  the difference between arrays, lists, and ArrayList

  The array is stored continuously in memory, so its index speed is very fast, and the assignment and modification elements are simple, such as:

string[] s=New string[3];//Assign Values[0]="a"; s[1]="b"; s[2]="C";//Modifys[1]="B1";

But there are some shortcomings in the array. For example, it is cumbersome to insert data between the two data in an array, and when we declare an array, we must also indicate the length of the array, the length of the array, the memory wasted, the array and the length too short, causing the data overflow error. So if we don't know the length of the array when we declare the array, it becomes very troublesome. ArrayList objects are first provided in C # to overcome these drawbacks.

ArrayList is a dedicated class provided by the. Net Framework for data storage and retrieval, which is part of the namespace System.Collections. Its size is dynamically expanded and shrunk according to the data stored in it. Therefore, we do not need to specify the length of the ArrayList object when declaring it. ArrayList inherits the IList interface, so it can be easily added, inserted, and removed from the data. For example:

ArrayList list =NewArrayList ();//New DataList. ADD ("ABC"); List. ADD (123);//Modifying Datalist[2] =345;//Removing DataList. RemoveAt (0);//Inserting DataList. Insert (0,"Hello World");

From the above example, ArrayList seems to solve all the shortcomings in the array, then it should be perfect, why the list after the c#2.0?

In list, we not only inserted the string "abc", but also inserted the number 123. This allows the insertion of different types of data in the ArrayList. Because ArrayList will handle all the data inserted into it as an object type. Thus, when we use the data in ArrayList to handle the problem, it is possible to report a type mismatch error, that is, ArrayList is not type-safe. Even though we are guaranteed to be careful when inserting data, we have inserted the same type of data, but in use, we also need to convert them to the corresponding primitive type to handle. This is the case of boxing and unpacking operation, will bring a lot of performance loss.

  the concept of boxing and unpacking : In simple terms: Boxing: The data of a value type is packaged into an instance of a reference type, such as assigning the value 123 of the int type to the object o

int i=123; Object O= (object) I;

Unpacking: Extracting value types from reference data such as assigning the value of the object o to the variable I of type int

Object o=123; int i= (int) o;

The process of packing and unpacking is very lossy.

It is because ArrayList has the disadvantage of unsafe type and packing unpacking, so the concept of generics appears after c#2.0. The list class is a generic equivalent class of the ArrayList class. Most of its usage is similar to ArrayList, because the list class inherits the IList interface as well. The key difference is that when declaring a list collection, we also need to declare the object type for the data in the list collection. Like what:

list<intnew list<int>(); // New Data list. ADD (123); //  list[0345; // removes the data list. RemoveAt (0);

In the example above, if we insert the string character "Hello World" into the list collection, the IDE will report an error and cannot be compiled. This avoids the previously mentioned type-safety issues and the performance issues of packing unboxing.

At the same time the list cannot be constructed, but a reference can be created to the list as above, and Listarray can be constructed.

List List;     // correct    List list=new list ();    //    is the wrong usage

List List = new ArrayList (); This sentence creates a ArrayList object that is traced back to list. At this point, it is a list object, some ArrayList have but the list does not have the properties and methods, it can no longer be used. and ArrayList list=new ArrayList (); Creating an object preserves all the properties of ArrayList.

Benefits of the list generics:
By allowing specific types of generic classes or method operations to be specified, generic functionality transfers type-safe tasks from you to the compiler. You do not need to write code to detect whether the data type is correct, because the correct data type is enforced at compile time. Reduces the need for type casting and the likelihood of run-time errors. Generics provide type-safe, but do not increase the overhead of multiple implementations.

The difference between arrays, lists, and ArrayList

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.