The difference between List and ArrayList

The reason for writing this article is that they no longer go wrong in disorderly fashion, these two things are used in this position:

List< String> list = new, ArrayList< > ();

But never carefully explored the relationship between List and ArrayList, today I based on the usual experience and on-line search of some information, a simple summary:

Array (array first, ArrayList and so on) and a series of optimizations

  • The array is stored continuously in memory, so its indexing speed is very fast, and the assignment and modification elements are simple
  • But at the same time, the array also has some disadvantages, such as insert in two data array data is very troublesome, and when the statement of the array must also indicate the length of the array, the array length is too long, will cause the waste of memory, the array length is too short, will cause the data overflow error. So the C# language provides ArrayList objects to overcome these shortcomings.

ArrayList

ArrayList is a private class for data storage and retrieval provided by.Net Framework, which is part of the namespace System.Collections. Its size is dynamically expanded and contracted in accordance with the data stored therein. So, we don’t need to specify the length of the ArrayList object when we declare it. ArrayList inherits the IList interface, so it makes it easy to add, insert, and remove data, for example:

ArrayList list = new (ArrayList); / / list.Add new data ("ACRS"); list.Add (123); / / modify data list[2] = 345; / / modify the index position of the value 2, the value was revised to 345; / / remove data list.RemoveAt (2); / / remove index to 2 data / / insert data list.Insert (0 "Hello world"); / / insert index position 0, "Hello world numerical"

From the above example, if ArrayList is to solve the disadvantage of all the array, but it also has some shortcomings, it is not the type of security and performance loss (due to the presence of packing, unpacking), so will appear in C#2.0 List object to add to it.
in the code, we not only insert the string “ACRS”, and insert the number 123. Thus, inserting different types of data into the ArrayList is allowed. Because ArrayList takes all the data that is inserted into it as a object type. Thus, when we use data in ArrayList to handle problems, it is likely to report a type mismatch error, that is to say, ArrayList is not type safe. Even though we guarantee that you are very careful when inserting data, you have the same type of data inserted, but when you use them, we also need to translate them into corresponding primitive types. This is the boxing and unboxing operations, will bring great loss of performance.

List

The List class is the generic equivalent class of the ArrayList class. Most of its usage is similar to that of ArrayList because the List class inherits the IList interface. The key difference is that when we declare the List collection, we also need to declare the object type of the data in the List collection. Such as:

List< int> list = new List< int> or; List< int>) (list = new; ArrayList< > (); / / list.Add new data (123); / / modify data list[0] = 345; / / remove the data of list.RemoveAt (0);

If we insert the string character “Hello world” into the List collection, IDE will make a mistake and cannot compile. This avoids the problem of performance type safety problems and speak in front of the boxing. That is, if you use List, then the data types within the collection have and can only be one, and not allow a variety of

in addition

List cannot be constructed, but you can create a reference for List as you do, and ListArray can be constructed
, that is:

List list list=null List list=new; / / right; (List); / / is the wrong usage of List< int> list = new; List< int> (List<); / / right; int> list = new ArrayList< > (); / / correct, note that this sentence; create an object ArrayList after the assignment to List. It is a List object, so there will be some ArrayList properties and methods of List but not List, then this object can not be reused ListArray listArray = new (ListArray); / / right here to create object retains all of the genus ArrayList

The concept of boxing and unboxing:

Boxing: to wrap data of value types into instances of reference types, such as assigning value int of type to object object o

Int i=123; object o= (object) i;

Unboxing: is from the reference data to extract value types such as the object o int to the value of a variable of type I

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

So the process of boxing and unboxing is loss properties.