The difference among array, ArrayList and List in C #
[quote Bob Wei's blog:https://www.cnblogs.com/BObwei/p/4869157.html ]
In C #, ArrayList and List can store a group of objects. What is the difference between the three.
array
Arrays are the first to appear in C#. It is stored continuously in memory, so the index speed is very fast, and the assignment and modification of elements are also very simple. The offset address can be used to access elements, and the time complexity is O(1); You can use the half search method to find elements with high efficiency.
string[] s=new string[3]; //assignment s[0]="a"; s[1]="b"; s[2]="c"; //modify s[1]="b1";
At the same time, arrays also have many disadvantages. The array is allocated on a continuous data space, so the size must be determined when allocating space. The continuity of space also leads to low storage efficiency, low efficiency of inserting and deleting elements, and trouble. If you want to add an element, you need to move a large number of elements, empty the space of an element in the memory, and then put the element to be added in it. Similarly, if you want to delete an element, you need to move a large number of elements to fill the moved elements.
In addition, when we declare an array, we must also specify the length of the array. If the length of the array is too long, it will cause a waste of memory. If the length of the array and is too short, it will cause a data overflow error. In this way, if we don't know the length of the array when declaring the array, it becomes very difficult.
In view of these shortcomings of arrays, ArrayList objects are first provided in C# to overcome these shortcomings.
ArrayList
ArrayList is a special class provided by the. Net Framework for data storage and retrieval. It is part of the namespace System.Collections. Its size is dynamically expanded and contracted 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 easily add, insert and remove data. For example:
ArrayList list = new ArrayList(); //New data list.Add("abc"); list.Add(123); //Modify data list[2] = 345; //Remove data list.RemoveAt(0); //insert data list.Insert(0, "hello world"); Get element value object value = al[index]; //al is an ArrayList object. Generally, value needs to be type converted, such as int n = (int)value; Set element value al[index] = value; //al is an ArrayList object, and index must be less than Count Append element int ArrayList.Add(object value) //Returns the index of the added element Insert element void ArrayList.Insert(int index, object value) Delete element After deleting an element, the following element moves forward, but Capacity It won't change. void ArrayList.Remove(object obj) //Search from the front (index 0) to the back, and delete the first element found that is the same as obj void ArrayList.RemoveAt(int index) //Delete the element corresponding to the index void ArrayList.RemoveRange(int index, int count) //Starting from the index, delete count elements Find element int ArrayList.IndexOf(object value) //Search from the front (index 0) to the back, and return the index of the first element found that is the same as obj int ArrayList.IndexOf(object value, int startIndex) int ArrayList.IndexOf(object value, int startIndex, int count) int ArrayList.LastIndexOf(object value) //Search from back to front (index 0) and return the index of the first element found that is the same as obj int ArrayList.LastIndexOf(object value, int startIndex) int ArrayList.LastIndexOf(object value, int startIndex, int count)
From the above example, ArrayList seems to solve all the shortcomings of arrays, so it should be perfect. Why does List appear after C#2.0?
From the above example, in the list, we not only insert the string "abc", but also insert the number 123. In this way, it is allowed to insert different types of data in ArrayList. Because ArrayList will treat all the data inserted into it as object type. In this way, when we use the data in ArrayList to deal with problems, we may report an error of type mismatch, that is, ArrayList is not type safe. Even if we ensure that we are very careful when inserting data and insert the same type of data, we also need to convert them into the corresponding original type for processing when using them. This leads to the operation of packing and unpacking, which will bring great performance loss.
Intersperse the concepts of packing and unpacking:
In short:
Boxing: packing data of value type into instances of reference type
For example, assign the int type value 123 to the object object o
int i=123; object o=(object)i;
Unpacking: extracting value types from reference data
For example, assign the value of object object o to variable i of type int
object o=123; int i=(int)o;
The process of packing and unpacking is very performance consuming.
Generic List
It is precisely because ArrayList has the disadvantages of unsafe types and boxing and unpacking that the concept of generics appeared after C#2.0. The List class is the generic equivalent of the ArrayList class. Most of its usage is similar to ArrayList because the List class also inherits the IList interface. The key difference is that when declaring the List collection, we also need to declare the object type of the data in the List collection.
List<int> list = new List<int>(); //New data list.Add(123); //Modify data list[0] = 345; //Remove data list.RemoveAt(0);
In the above example, if we insert the string character "hello world" into the List set, the IDE will report an error and cannot be compiled. In this way, the type safety problem and the performance problem of packing and unpacking mentioned above are avoided.
Console.WriteLine("List Test:"); //Declare an integer List List<int> lsTest = new List<int>(); lsTest.Add(7); lsTest.Add(5); lsTest.Add(1); lsTest.Add(3); string strTest=""; //list sorting lsTest.Sort(); //Traversal of list foreach(int i in lsTest) strTest+=i.ToString()+" "; //Output after formatting Console.Write(string.Format("Out:{0} nCount:{1}n",strTest,lsTest.Count)); //Read the next key so that the data is displayed on the screen Console.ReadKey();
The results are as follows
Program code List Test: Out:1 3 5 7 Count:4