当前位置:网站首页>Differences and performance of array, list, ArrayList, directory and LinkedList in C #

Differences and performance of array, list, ArrayList, directory and LinkedList in C #

2022-06-22 14:36:00 Singing folk songs on the grassland

Array int[] count = new int[10];
ListList<int> list = new List<int>;
ArrayListArrayList arrayList = new ArrayList();
DictionaryDictionary<string, int> dire = new Dictionary<string, int>();
LinkedListLinkedlList<string> linkedList = new LinkedList<string>();

analysis :
From the above initialization, we can see , These types are all reference types . Where array 、ListDictionary and LinkedList The element type needs to be specified during initialization ( The array also needs to specify its size ), and ArrayList There is no need to specify its type during initialization .

Array : Its size and type must be specified during initialization , It is stored continuously in memory , So we can see that the indexing speed of the array is very fast . After determining the length and type of the array , Choosing an array to store data is a good choice . Not suitable for insert operation .

List: The data type of the objects in the collection must be specified during initialization , But you don't need to specify the size , It won't look like ArraryList That will cause packing and unpacking operations in the process of access , Avoid type insecurity . In the case of the same type ,List And the performance of array . Easy to insert .

ArrayList:ArrayList stay System.Collections Under the namespace , At the same time inherited IList Interface .ArrayList The size of the object can be dynamically increased or decreased according to the stored data , There is no need to specify its size and type during initialization . It is troublesome to insert data between two data for an array , And its size must be determined during initialization , therefore C# Provide ArrayList Overcome these shortcomings , It can store different data types , All data accessed will be converted into object type , Easy to insert , But when accessing and retrieving value types , It will cause packing and unpacking , Reduced performance , meanwhile ArrayList There is no implementation of generics , It's not type safe , When processing data, it is very likely to produce data mismatch errors .

Dictionary: Its type must also be specified during initialization , And it also needs to specify a Key, And this Key Is the only one. . Because of this ,Dictionary The indexing speed of is very fast . But also because it adds a Key,Dictionary It takes up more memory space than other types . It's through Key To find the , The order of the elements is indeterminate .

LinkedList: Both arrays and array lists have a major flaw , This is the cost of removing an element from the middle of the array , The reason is that all elements in the array after the deleted elements are moved to the front of the array . The same goes for inserting an element in the middle of an array .

原网站

版权声明
本文为[Singing folk songs on the grassland]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221130306833.html