- Array
- ArrayList
- Generic List
- Summarize
in C # arrays, Arraylist,list can store a set of objects, so what is the difference between the three? (1) namespaces introduced by arrays:using System; (2) array: Use basic with arrays, introduce namespaces:using System; (3) ArrayList: Introducing namespaces: Using System.Collections(4) List: Introduction namespace:using System. Collections. Generic; Array
Arrays are first seen in C #. is stored continuously in memory, so its index is very fast, and the assignment and modification elements are simple.
[CSharp]View Plaincopy print?
- <span style="Font-family:simsun; font-size:18px; " >//Arrays
- String[] s=new string[2];
- Assign value
- s[0]="a";
- s[1]="B";
- Modify
- s[1]="A1";
- </span>
Array string[] s=new string[2];//assignment s[0]= "a"; s[1]= "B";//Modify s[1]= "A1";
But there are some deficiencies in the array. Inserting data between the two data in an array is cumbersome, and when declaring an array, you must specify the length of the array, the length of the array is too long, and the memory is wasted, which can cause data overflow errors. If we do not know the length of the array when declaring an array, it becomes cumbersome.
For these drawbacks of arrays, ArrayList objects are first provided in C # to overcome these drawbacks.
ArrayList
ArrayList is part of the namespace System.Collections and must be referenced when using the class, inheriting the IList interface, providing data storage and retrieval. The size of the ArrayList object is dynamically expanded and shrunk according to the data stored in it. Therefore, you do not need to specify the length of the ArrayList object when declaring it.
[CSharp]View Plaincopy print?
- <span style="Font-family:simsun; font-size:18px; " >//arraylist
- ArrayList List1 = new ArrayList ();
- New data
- List1. ADD ("CDE");
- List1. ADD (5678);
- modifying data
- List[2] = 34;
- removing data
- List. RemoveAt (0);
- Inserting data
- List. Insert (0, "qwe");
- </span>
Arraylistarraylist List1 = new ArrayList ();//New Data List1. ADD ("CDE"); List1. ADD (5678);//modify data list[2] = 34;//Remove data list. RemoveAt (0);//Insert Data list. Insert (0, "qwe");
From the above example, ArrayList seems to solve all the shortcomings in the array, and why is there a list?
As we see from the above example, in list, we not only inserted the string CDE, but also inserted the number 5678. This allows the insertion of different types of data in the ArrayList. Because ArrayList will treat all of the data inserted as object types, when we use ArrayList to process data, it is possible to report a type mismatch error, that is, ArrayList is not type-safe. Boxing and unboxing operations typically occur when a value type is stored or retrieved, resulting in significant performance wear and tear.
The concept of boxing and unpacking: simply: Boxing: The data of a value type is packaged into an instance of a reference type such as assigning the value ABC of type string to the object obj
[CSharp]View Plaincopy print?
- <span style="Font-family:simsun; font-size:18px; " >string i= "ABC";
- Object Obj= (object) i;
- </span>
String i= "ABC"; object obj= (object) I;
Unpacking: Extracting value types from reference data such as assigning the value of the object obj to the variable I of type string
[CSharp]View Plaincopy print?
- <span style="Font-family:simsun; font-size:18px; " >Object obj= "abc";
- String i= (string) obj;
- </span>
Object obj= "ABC"; string i= (string) obj;
The process of packing and unpacking is very lossy.
Add:
Array is used almost as an array, and can be seen as an array. The length must be specified at the time of definition, and the use of ArrayList is the same as a normal set, which does not require a specified length;
such as: array[] Animalarray = new array[2];
ArrayList animalarraylist = new ArrayList ();
Generic list
The concept of generics occurs because ArrayList has the disadvantage of unsafe types and packing unboxing. The list class is a generic equivalent class of the ArrayList class, and 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:
[CSharp]View Plaincopy print?
- <span style= "font-family: simsun; font-size: 18px; " >list<string> list = new list<< span class= "keyword" >string> ();
-
- //new data
- list. ADD ("abc");
- &NBSP;&NBSP;
- //modify data
- list[0] = "Def";
-
- < span class= "comment" >//remove data
- list. RemoveAt (0);
- </SPAN>&NBSP;&NBSP;
list<string> list = new list<string> ();//New data list. ADD ("abc");//modify data List[0] = "def";//Remove Data list. RemoveAt (0);
In the example above, if we insert an int array into the list collection 123,ide will report an error and cannot be compiled. This avoids the previously mentioned type-safety issues and the performance issues of packing unboxing.
Add:
Summary:
The capacity of an array is fixed, you can only get or set the value of one element at a time, and the capacity of ArrayList or list<t> can automatically expand, modify, delete, or insert data as needed.
An array can have multiple dimensions, and ArrayList or list< t> always have only one dimension. However, you can easily create lists of array lists or lists. The performance of arrays of specific types (except Object) is better than ArrayList. This is because the elements of ArrayList are of type Object, so boxing and unboxing operations typically occur when a value type is stored or retrieved. However, when redistribution is not required (that is, the initial capacity is very close to the maximum capacity of the list),list< t> has the same performance as an array of the same type.
When deciding whether to use List<t> or the ArrayList class, which has similar functionality, remember that the List<t> class performs better and is type-safe in most cases. If you use a reference type for type T of the list< t> class, the behavior of the two classes is exactly the same. However, if you use value types for type T, you need to consider implementation and boxing issues.
Array: (1) When declaring an array, you must specify the size of the array, waste memory too long, and a short memory overflow (2) It is troublesome to insert data between two numbers in the array.
ArrayList (The IList Interface is inherited): can be inserted into different value types, in the data retrieval and storage of the existence of boxing and unpacking operations, easy to bring performance consumption.
List (which inherits the IList interface): When declaring a list collection, we also need to declare the object type of the data in the list collection for it , avoiding boxing and unboxing.
Array, ArrayList, list differences in C #