The difference between arrays, ArrayList, lists, LinkedList

Source: Internet
Author: User

One, array

Arrays are stored continuously in memory, so the index is very fast, and it is easy to assign and modify elements.

1. One-dimensional arrays

Declares an array:
int[] array = new INT[5];

Initializes an array of:
int[] array1 = new Int[5] {1, 3, 5, 7, 9}; Fixed length


Declare and initialize:
Int[] Array2 = {1, 3, 5, 7, 9}; Indefinite length


2. Multidimensional arrays
int[,] numbers = new int[3, 2] {{1, 2}, {3, 4}, {5, 6}};

But there are some deficiencies in the array. Inserting data between 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, the memory is wasted, and too short 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. NET to overcome these drawbacks.

Second, 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.

ArrayList List1 = new ArrayList ();    Added data  List1. ADD ("CDE");  List1. ADD (5678);    Modified data  list[2] =;    Removes the data  list. RemoveAt (0);    Insert Data  list. Insert (0, "qwe");  

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.

Add: Array expansion   This is a factor that has a greater impact on ArrayList efficiency.   whenever you do add, AddRange, Insert, Insertrange, and so on, the capacity of the internal array is not enough, and if it is, it will rebuild an array at twice times the current capacity, copy the old elements into the new array, Then discard the old array, at this point in the expansion operation, it should be compared to the impact of efficiency. Example 1: For example, a data that may have 200 elements is dynamically added to a ArrayList created with the default 16 element size, will pass through: 16*2*2*2*2 = 2,564 times the expansion will meet the final requirements, then if you start with: ArrayList List = New ArrayList (210); The way to create ArrayList will not only reduce the array creation and copy operations by 4 times, but also reduce memory usage. Example 2:30 elements are expected to create a arraylist:arraylist List = new ArrayList (30); In the process, add 31 elements, then the array will be expanded to 60 elements of the size, and this time there will be no new elements added, and there is no call to the Trimsize method, then there is 1 expansion of the operation, and wasted 29 element size space. If this is the case, use: ArrayList List = new ArrayList (40); Then everything has been solved. Therefore, it is an important way to improve the efficiency of ArrayList use to correctly estimate the possible elements and call the Trimsize method when appropriate.


Third, 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.
list<string> list = new list<string> ();  New Data  list. ADD ("abc");  Modify Data  list[0] = "def";  Removes the data  list. RemoveAt (0);  
Iv. LinkedList

However, there is a significant flaw in arrays and array lists, which is that removing an element from the middle of an array takes a significant cost because all elements in the array that are behind the deleted element are moved to the front end of the array. Inserting an element in the middle of the array is also true. Such as:

This problem is solved by LinkedList (linked list). The list stores each object in a separate node, and each node holds a reference to the previous node in the sequence and a reference to the next node, such as:

In this way, it is easy to delete an element from the middle of the list, that is, you need to update the node near the delete element, such as:

List<string> names=new linkedlist<> () names.add ("Amy"); Names.add ("Bob");

Summarize:

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.

ArrayList is the realization of the data structure based on dynamic array, LinkedList data structure based on the linked list.

For random access get and set,arraylist feel better than LinkedList because LinkedList wants to move the pointer.

Add and remove,linedlist are the dominant for new and deleted operations because ArrayList is moving the data.

Source: https://www.cnblogs.com/janneystory/p/5758958.html

Differences between arrays, ArrayList, lists, LinkedList

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.