[Personal Learning]: C # data structure comparison of Array, ArrayList, List, LinkedList

A, Array Array

  1.Array collection Introduction

  • Class is the base array array System.Object, it is a reference type;
  • array data stored in memory array contiguous and specified when defining the required length;
  • array type storage array: the same type;
  • array access array form: fast query the index as: arr [2];

    2. pros and cons

  • Array in memory are stored continuously, so his index very fast access; (advantage)
  • Array type is the same, the process does not involve stored boxing and unboxing (advantage)
  • An array of fixed-length, non-fixed-length data processing more difficult ( array length is too long, will result in wasted memory, the number of head through short, cause data overflow ) (disadvantage);
  • Slow insertion efficiency (data are stored continuously) (bad);

  3. Example

            // the Array Array 
            The Stopwatch StopWatch = new new The Stopwatch (); // Timer: means for accurate measurement of the running time 
            stopWatch.Start ();
            Console.WriteLine ( " start time initialize the array: " );
             int [] = intArray new new  int [ 10000000 ];
             for ( int I = 0 ; I < 10000000 ; I ++ )
            {
                intArray[i] = i + 1;
            }
            stopWatch.Stop();
            Console.WriteLine ($ " initializes the array length int took a total of 10,000,000 {stopWatch.ElapsedMilliseconds.ToString ()} milliseconds! " ); 
       // initialize the length of the array int 1,000,000 Total time: 63 ms;

Two, ArrayList array

  1.ArrayList collection Introduction

  • Array ArrayList is an upgraded version;
  • ArrayList is a special class of .Net Framework provides for data storage and retrieval, which is part of the namespace System.Collections.
  • ArrayList in accordance with the size of the data stored therein to the dynamic expansion and contraction.

 

  2. pros and cons

  • Dynamic expansion, does not need to specify its length ArrayList object declaration , not worry about the length of the overflow; (advantage)
  • Can store any data type; (advantage)
  • Query speed; (advantage)
  • Array more flexibility than the Array, but at the expense of performance on the basis of;
  • Dynamic expansion is paid sacrificing performance, beyond the default length after the ArrayList, automatically expansion; (disadvantage)
  • Storing any type of cost: there packing and unpacking data is accessed, affecting the efficiency; (disadvantage)
  • ArrayList not type safe
    • It is likely to be reported when the type mismatch problem either int or string are treated as object data storage to deal with, so we'll use ArrayList data;
    • Even guaranteed to remain consistent as the data type of data is inserted, but also when they need to be used to convert the original type (boxing and unboxing), causing performance loss;

  3. Example

            // the ArrayList array 
            The Stopwatch StopWatch = new new The Stopwatch (); // Timer: means for accurate measurement of the running time 
            stopWatch.Start ();
            Console.WriteLine ( " start time initialization ArrayList array: " );
            ArrListByLength the ArrayList = new new the ArrayList ( 10000000 ); // the specified array length
            
            for (int i = 0; i < 10000000; i++)
            {
                arrListByLength.Add(i + 1);
            }
            stopWatch.Stop();
            Console.WriteLine ($ " ArryList collection took a total of 10,000,000 memory length {stopWatch.ElapsedMilliseconds.ToString ()} milliseconds! " );

            stopWatch.Restart();
            ArrList the ArrayList = new new the ArrayList (); // do not specify the length of the array 
            for ( int I = 0 ; I < 10000000 ; I ++ )
            {
                arrList.Add(i + 1);
            }
            stopWatch.Stop();
            Console.WriteLine ($ " ArryList collection does not specify a total length of storage time consuming {stopWatch.ElapsedMilliseconds.ToString ()} milliseconds! " );
             // the ArrayList set specified storage Total time length of 110 ms 10000000!
            // the ArrayList set is not specified memory length 135 ms Total time!

 

 

Three, List Array

  1.List collection Introduction

  • List is an upgraded version of ArrayList, List and ArrayList are inherited from IList, use essentially the same, their biggest difference is that when you declare List collection, you need to specify the type of data collection within    

  2. pros and cons

  • Array retains the advantages of avoiding the type of data within the set of unsafe performance cost of boxing and unboxing (advantage);
  • Automatic expansion performance cost (disadvantage);

  3. Example

   

            // List generic 
            The Stopwatch StopWatch = new new The Stopwatch (); // Timer: means for accurate measurement of the running time 
            stopWatch.Start ();
            List < int > List = new new List < int > ( 10000000 ); // specified array length 
            Console.WriteLine ( " start time initialization Generic Array List: " );
             for ( int I = 0 ; I < 10000000 ; I ++ )
            {
                list.Add(1 + 1);
            }
            stopWatch.Stop();
            Console.WriteLine ($ " List generic collection took a total of 10,000,000 memory length {stopWatch.ElapsedMilliseconds.ToString ()} milliseconds! " );

            stopWatch.Restart();

            List = new new List < int > (); // do not specify the length of the array 
            for ( int I = 0 ; I < 10000000 ; I ++ )
            {
                list.Add(1 + 1);
            }
            stopWatch.Stop();
            Console.WriteLine ($ " List do not specify a set of generic memory length Total time {stopWatch.ElapsedMilliseconds.ToString ()} milliseconds! " );
             // List specifies a set of generic memory length of 80 ms Total time 10,000,000!
            // List do not specify a set of generic memory length 110 ms Total time!

 

 

Four, LinkedList array

  1. LinkedList collection Introduction

  • LinkedList achieve bidirectional linked list, in the LinkedList, each element pointing to the next element to form a chain.
  • LinkedList discontinuous storage;
  • Support insert from both the head and tail;
  • Its length is variable;

 

     2. pros and cons

  • Discontinuous storage, insertion and deletion of high efficiency; (advantage)
  • Fixed length, regardless of the length of the initialization; (advantage)
  • Supports simultaneous insertion head and tail; (advantage)
  • Safety data type, specify the data type (advantage) when you create
  • Low query efficiency, can not be indexed access (shortcomings) 

    3. Example

            // the LinkedList list 
            The Stopwatch StopWatch = new new The Stopwatch (); // Timer: means for accurate measurement of the running time 
            stopWatch.Start ();
            LinkedList<int> list = new LinkedList<int>();//指定数组长度
            Console.WriteLine("开始计时初始化LinkedList链表数组:");
            for (int i = 0; i < 10000000; i++)
            {
                list.AddFirst(1 + 1);
            }
            stopWatch.Stop();
            Console.WriteLine($"LinkedList链表集合共耗时{stopWatch.ElapsedMilliseconds.ToString()}毫秒!");
            //LinkedList链表集合不指定存储长度共耗时1934毫秒!

 

五、结论

  • Array效率最高,List次之,ArrayList紧随其后,LinkedList效率最低;
  • ArrayList和List,在定义时如果知道其数据长度,那么初始化时指定,效率相对比不指定长度高;

 

 

Guess you like

Origin www.cnblogs.com/jazzj/p/11875952.html