c. comparative analysis of Array, ArrayList, List and LinkedList of data structure

Keywords: C#

I. Preface:

In the c ා data structure, the application of collection is very extensive. No matter for BS architecture or CS architecture development, it is inseparable from the use of collection. For example, our common collection includes: Array, ArrayList, List, LinkedList, etc. These data sets can be accessed functionally, but what are the differences among them and what should be paid attention to when using them? Based on personal experience, I will make a simple summary of the use of these collection data. If it's not right, please point out and exchange more improvements.

II. Introduction to Array set

Array set, also known as array, is the simplest data structure. The data stored in array is continuous in memory space. Array has some characteristics

  • 1. Data storage is continuous
  • 2. Array length must be defined
  • 3. The data types stored in the array are all of the same type
  • 4. Arrays can be accessed directly through small labels

Advantages and disadvantages:
Advantages:
1. It can be accessed directly according to the index, with fast access speed.
2. The data is safe. Due to the consistency of data types, it is not involved in the storage and use process
Disadvantages:
1. Because the data is stored continuously, the insertion efficiency slows down
2. Because the length of the array is fixed, it is not easy to deal with the expected non fixed length numbers

Exercise instance code:

 

    /// <summary>
    /// Array practice operation 
    /// </summary>
    public class ArrayTest
    {
        /// array Array It's nothing new to you
        /// Array is the storage space allocated continuously in memory, which also leads to the following characteristics of array
        /// 1.Data storage is continuous
        /// 2.Array length must be defined
        /// 3.Array stores the same data type
        /// 4.Arrays can be accessed directly through small labels
        /// 
        /// Advantages and disadvantages:
        /// Advantage:
        ///     1,It can be accessed directly according to the index, with fast access speed
        ///     2,The data is safe. Due to the consistency of data types, the operation of packing and unpacking is not involved in the process of storage and use.
        /// Disadvantages:
        ///     1,Because the data is stored continuously, the insertion efficiency becomes slower
        ///     2,Because the length of the array is fixed, the number of the expected non fixed length is not easy to handle

        /// int Array operation of type 
        public static void IntArrayTest()
        {

            //// Define a stopwatch, execute to get execution time
            Stopwatch st = new Stopwatch();//Instantiated class
            st.Start();//Start timing

            Console.WriteLine("Start initializing 10000000 int Array:");

            //// Define an array
            int[] nums = new int[10000000];
            for (int i = 0; i < 10000000; i++)
            {
                nums[i] = 1 + 1;
            }

            //Code snippet that needs to count time

            st.Stop();//Termination of time
            Console.WriteLine(string.Format("Initialization length 10000 int Array complete! Total time consuming{0}Millisecond", st.ElapsedMilliseconds.ToString()));
        }
    }

 

III. introduction to ArrayList collection

ArrayList is an upgraded version of Array, which can solve some shortcomings of Array
The internal implementation of ArrayList is also Array, but its length can be dynamic. A variable is used to record and control the length. ArrayList has the following characteristics

  • 1. The length is not fixed
  • 2. Different data types (objects) can be stored
  • 3. Index query is also supported (it can be accessed directly through small labels)
  • 4. More flexibility at the expense of performance

Advantages and disadvantages:
Advantage:
1. The length is not fixed. In the definition, it is unnecessary to bear the length overflow
2. Any data type can be stored
3. It can be queried according to the index, with fast query efficiency
Disadvantages:
1. Because the length is not fixed, the execution efficiency is low, because after exceeding the default length (10), the data will be automatically expanded and copied, sacrificing performance
2. Because the storage type is object, there will be packing operation when storing data and unpacking operation when fetching data, which will affect efficiency
3. Thread is not safe, because its internal implementation is controlled by size and array, and it is a non atomic operation when adding an operation, so it is not a safe thread

Tips:
In the actual use process, in order to avoid automatic capacity expansion, the data length can be estimated, and a data length can be initialized, so as to improve efficiency

Exercise instance code:

    /// <summary>
    /// ArrayList Array practice operation 
    /// </summary>
    public class ArrayListTest
    {
        /// ArrayList yes Array The upgrade version of can solve Array Some shortcomings of
        /// ArrayList Its internal implementation is also Array,But its length can be dynamic, and a variable is used to record and control the length, ArrayList It has the following characteristics
        /// 1.Length is not fixed
        /// 2.Different data types can be stored(object)
        /// 3.Index query is also supported (it can be accessed directly through small labels)
        /// 4.More flexibility at the expense of performance

        /// Advantages and disadvantages:
        /// Advantage:
        ///     1,The length is not fixed. It is not necessary to bear the length overflow in the definition
        ///     2,Can store any data type
        ///     3,It can be queried according to the index, and the query efficiency is fast
        /// Disadvantages:
        ///     1,Because the length is not fixed, the execution efficiency is low, because after exceeding the default length (10), the copy data will be automatically expanded, sacrificing performance
        ///     2,Because the storage type is object,So there will be packing operation when storing data and unpacking operation when fetching data, which will affect efficiency
        ///     3,Thread is not safe because its internal implementation uses size,array It is a non atomic operation, so it is not a safe thread
        ///     
        /// Tips:
        ///     In the actual use process, in order to avoid automatic capacity expansion, the data length can be estimated, and a data length can be initialized, so as to improve efficiency

        /// ArrayList Operation example
        public static void ArrayListOpert()
        {

            //// Define a stopwatch, execute to get execution time
            Stopwatch st = new Stopwatch();//Instantiated class

            //// Code snippet that needs to count time(Count execution time at initialization length)
            st.Start();//Start timing
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("ArryList The amount of data stored in the collection is 10000000, initializing a length, and execution starts:");

            ArrayList arrayList = new ArrayList(10000000);

            //// Define an array
            for (int i = 0; i < 10000000; i++)
            {
                arrayList.Add(1 + 1);
            }

            st.Stop();//Termination of time
            Console.WriteLine(string.Format("ArryList The amount of data stored in the collection is 10000000, initializing a length, execution completed:! Total time consuming{0}Millisecond", st.ElapsedMilliseconds.ToString()));

            //// Code snippet that needs to count time(Count execution time when initializing a non specified length)

            st.Restart();
            Console.WriteLine("");
            Console.WriteLine("ArryList The amount of data stored in the collection is 10000000, the initialization does not specify the length, and the execution starts:");

            arrayList = new ArrayList();

            //// Define an array
            for (int i = 0; i < 10000000; i++)
            {
                arrayList.Add(1 + 1);
            }

            st.Stop();//Termination of time
            Console.WriteLine(string.Format("ArryList The amount of data stored in the collection is 10000000, initialization does not specify the length, execution completed:! Total time consuming{0}Millisecond", st.ElapsedMilliseconds.ToString()));
        }
    }

 

IV. introduction to List set

 

With the introduction of c ා generics, in order to avoid some disadvantages of ArrayList, Microsoft launched List collection
The List collection is still implemented in Array, and the corresponding data type needs to be specified when defining
This level preserves the advantages of the Array collection, and avoids the performance sacrifice caused by the insecure data types and boxing of the ArrayList collection
List features:

  • 1. The data length is not fixed, and it will be increased automatically
  • 2. Store the same data type
  • 3. It can be queried according to the index, with fast query efficiency

Advantages and disadvantages:
Advantage:
1. The length is not fixed. In the definition, it is unnecessary to bear the length overflow
2. Store data of the same data type, avoid data packing and unpacking, and improve data processing efficiency
3. It supports index query with high efficiency
Disadvantages:
1. Because the length is not fixed, the execution efficiency is low, because after exceeding the default length (10), the data will be automatically expanded and copied, sacrificing performance
2. Thread is not safe, because its internal implementation is controlled by size and array, and it is a non atomic operation when adding an operation, so it is not a safe thread

 

Exercise instance code:

   /// <summary>
    /// List Practice operation
    /// </summary>
    public class ListTest
    {
        /// along with c#In order to avoid some disadvantages of ArrayList, Microsoft launched List collection
        /// List It's still used inside the collection Array Implementation, and the corresponding data type needs to be specified when defining
        /// This level is reserved Array The advantages of aggregation are also avoided ArrayList Unsafe data types of collections and performance sacrifice caused by boxing
        /// List Characteristic:
        /// 1,Data length is not fixed, and it will be increased automatically
        /// 2,Store the same data type
        /// 3,It can be queried according to the index, and the query efficiency is fast
        /// 
        /// Advantages and disadvantages:
        /// Advantage:
        ///     1,The length is not fixed. It is not necessary to bear the length overflow in the definition
        ///     2,Store data of the same data type, avoid data packing and unpacking, and improve data processing efficiency
        ///     3,Support index query, fast query efficiency
        /// Disadvantages:
        ///     1,Because the length is not fixed, the execution efficiency is low, because after exceeding the default length (10), the copy data will be automatically expanded, sacrificing performance
        ///     2,Thread is not safe because its internal implementation uses size,array It is a non atomic operation, so it is not a safe thread

        /// ArrayList Operation example
        public static void ListOpert()
        {
            //// Define a stopwatch, execute to get execution time
            Stopwatch st = new Stopwatch();//Instantiated class
            st.Start();//Start timing
                       //// Code snippet that needs to count time(Count execution time at initialization length)
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("List The amount of data stored in the collection is 10000000, initializing a length, and execution starts:");

            List<int> list = new List<int>(10000000);

            //// Define an array
            for (int i = 0; i < 10000000; i++)
            {
                list.Add(1 + 1);
            }

            //Code snippet that needs to count time

            st.Stop();//Termination of time
            Console.WriteLine(string.Format("List The amount of data stored in the collection is 10000000, initializing a length, execution completed:! Total time consuming{0}Millisecond", st.ElapsedMilliseconds.ToString()));

            //// Code snippet that needs to count time(Count execution time when initializing a non specified length)
            st.Restart();
            Console.WriteLine("");
            Console.WriteLine("List The amount of data stored in the collection is 10000000, the initialization does not specify the length, and the execution starts:");

            list = new List<int>();

            //// Define an array
            for (int i = 0; i < 10000000; i++)
            {
                list.Add(1 + 1);
            }

            st.Stop();//Termination of time
            Console.WriteLine(string.Format("List The amount of data stored in the collection is 10000000, initialization does not specify the length, execution completed:! Total time consuming{0}Millisecond", st.ElapsedMilliseconds.ToString()));
        }
    }

 

V. introduction to LinkedList

 

 

The bottom layer of linked list is implemented in the way of two-way linked list,
In the Linked List, each element points to the next element to form a chain
Data can be inserted from the head and tail, and stored in memory in a discontinuous way. The linked list has the following characteristics

  • 1. Memory storage is discontinuous
  • 2. It can support simultaneous insertion from the head and the bottom
  • 3. The length is not fixed

Advantages and disadvantages:
Advantage:
1. Because of discontinuous storage, the efficiency of inserting and deleting elements in the middle is high
2. The length is not fixed, so it is not necessary to consider its length when creating
3. Elements can be added to punch part and bottom
4. The data type is safe and needs to be specified during creation
Disadvantages:
1. Due to discontinuous storage, it can't be accessed through small labels, so the query efficiency is low

Exercise instance code:

    /// <summary>
    /// LinkedList Practice operation
    /// </summary>
    public class LinkedListTest {
        /// LinkedList The bottom layer of the linked list is realized by the way of two-way linked list,
        /// In the linked list ( Linked List)In, each element points to the next element to form a chain( chain)
        /// Data can be inserted from the head and tail, and stored in memory in a discontinuous way. The linked list has the following characteristics
        /// 1,Discontinuous in memory storage
        /// 2,Able to support simultaneous insertion from head and bottom
        /// 3,Length is not fixed
        /// Advantages and disadvantages:
        /// Advantage:
        ///     1,Efficient insertion and deletion of elements in the middle due to discontinuous storage
        ///     2,The length is not fixed, so it is not necessary to consider its length when creating
        ///     3,Elements can be added to the punch and bottom
        ///     4,The data type is secure and needs to be specified when it is created
        ///  Disadvantages:
        ///     1,Because of discontinuous storage, it can't be accessed through small label, so the query efficiency is low

        /// LinkedList Operation example
        public static void LinkedListTestOpert() {
            //// Define a stopwatch, execute to get execution time
            Stopwatch st = new Stopwatch();//Instantiated class
            st.Start();//Start timing
                       //// Code snippet that needs to count time(Count execution time at initialization length)
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("Linked The amount of data stored in the collection is 10000000, and the execution starts:");

            LinkedList<int> list = new LinkedList<int>();

            //// Define an array
            for (int i = 0; i < 10000000; i++)
            {
                list.AddFirst(1 + 1);
            }

            //Code snippet that needs to count time

            st.Stop();//Termination of time
            Console.WriteLine(string.Format("Linked The amount of data stored in the collection is 10000000, and the execution is finished:! Total time consuming{0}Millisecond", st.ElapsedMilliseconds.ToString()));
        }
    }

 

Vi. comparison and analysis of the results of not collecting data

 

    class Program
    {
        static void Main(string[] args)
        {
            //// array Array operation test
            ArrayTest.IntArrayTest();

            //// arrayList Set operation test
            ArrayListTest.ArrayListOpert();

            //// List Set operation test
            ListTest.ListOpert();

            //// LinkedList Set operation test
            LinkedListTest.LinkedListTestOpert();

            ///// Pass test data
            //Some conclusions can be drawn from the test data
            //1,Overall efficiency Array Most efficient, ArrayList The lowest efficiency, List Efficiency between Array and ArrayList Between
            //2,ArrayList and List Set. If the data length is known at the time of definition, the efficiency of the specified length is higher than that of the unspecified length at initialization.
            
            
            //Conclusion:
            //The following suggestions are given on the selection of data set use:
            //1,Array: When the number of elements is fixed and subscripts are needed
            //2,ArrayList: When the stored element types are different
            //3,List: When the number of elements is fixed and subscripts are needed
            //4,LinkedList: When elements need to be able to be added at both ends of the list
            Console.ReadLine();
        }
    }

Execution result data

 

Some conclusions can be drawn from the test data
1. In terms of overall efficiency, the Array efficiency is the highest and the ArrayList efficiency is the lowest. The List efficiency is between Array and ArrayList
2. ArrayList and List collection. If the data length is known during definition, the efficiency of the specified length is higher than that of the unspecified length during initialization

7. Summary:


The following suggestions are given on the selection of data set use:
1. Array: when the number of elements is fixed and subscripts are needed
2. ArrayList: when the stored element types are different, an estimated length is given during initialization
3. List: when the number of elements is fixed and subscripts are needed, an estimated length is given during initialization
4. LinkedList: when an element needs to be added at both ends of the list

Posted by suzzane2020 on Fri, 15 Nov 2019 00:43:18 -0800