The difference between Array, ArrayList, List, LinkedList

an array

Arrays are stored contiguously in memory, so indexing is very fast, and assigning and modifying elements is simple

二 ArrayList

The size of the ArrayList object is dynamically expanded and shrunk according to the data stored in it. It is not necessary to specify its length when declaring the ArrayList object.

Whenever the method of adding elements such as Add, AddRange, Insert, InsertRange is executed, it will check whether the capacity of the internal array is not enough. If so, it will rebuild an array with twice the current capacity, and copy the old elements to the new ones. In the array, and then discard the old array, the expansion operation at this critical point should be more efficient.

Example 1: For example, a data that may have 200 elements dynamically added to an ArrayList created with the default size of 16 elements will go through:
16*2*2*2*2 = 256.

Four expansions will meet the final requirements, so if you create an ArrayList in the way of:
ArrayList List = new ArrayList( 210 ); at the beginning
, it will not only reduce the 4 times of array creation and copy operations, but also reduce memory usage.
Example 2: An ArrayList is created with an expectation of 30 elements:
ArrayList List = new ArrayList(30);
In the process, 31 elements are added, then the array will expand to the size of 60 elements, and at this time there will be no If new elements are added, and the TrimSize method is not called, then there will be one expansion operation, and the space of 29 elements will be wasted. If at this time, use:
ArrayList List = new ArrayList(40);
Then everything is solved.
Therefore, correctly estimating the possible elements and calling the TrimSize method at the appropriate time is an important way to improve the efficiency of ArrayList usage.
The useless value of ArrayList is not null, but ArrayList will pre-apply for a little more space each time it grows, 1.5 times +1, not twice
. This will appear when size() = 1000, ArrayList has already applied for it In the case of 1200 space,
the function of trimToSize is to remove the reserved element position, that is, to delete the excess 200, and change to only apply for 1000, which will be used when the memory is tight.
Three Lists
because ArrayList has the shortcomings of unsafe types and boxing and unboxing, Hence the concept of generics. The List class is the generic equivalent of the ArrayList class, and most of its usage is similar to ArrayList because the List class also inherits the IList interface. The most crucial difference is that,
When declaring the List collection, we also need to declare the object type of the data in the List collection for it.

4. LinkedList
However, both arrays and array lists have a major flaw, which is that deleting an element from the middle of the array requires a lot of money. The reason is that all elements in the array after the deleted element must be sent to the array. Front end moves. Inserting an element in the middle of the array is the same.
This problem is solved by LinkedList. The linked list stores each object in a separate node, and each node also stores a reference to the previous node and a reference to the next node in the sequence. In
this way, deleting an element from the middle of the linked list is a very easy operation, that is, it needs to be deleted. It can be
concluded by updating the nodes near the element :
the capacity of the array is fixed, and the value of one element can only be obtained or set at a time, while the capacity of an ArrayList or List<T> can be automatically expanded, modified, deleted or inserted as needed.
Arrays can have multiple dimensions, whereas an ArrayList or List<T> always has only one dimension. However, it is easy to create an array list or a list of lists. Arrays of certain types (except Object) perform better than ArrayList.
This is because the elements of an ArrayList are of type Object;
so boxing and unboxing operations typically occur when storing or retrieving value types. However, when reallocation is not required (that is, the initial capacity is very close to the maximum capacity of the list), List<T> performs very similar to an array of the same type.
When deciding whether to use List<T> or the ArrayList class (both have similar functionality), keep in mind that the List<T> class performs better and is type-safe in most cases. if for List<T> The type T of the class uses a reference type, the behavior of the two classes is identical.
However, if you use a value type for type T, you need to consider implementation and boxing issues.
ArrayList is a data structure based on dynamic arrays, and LinkedList is a data structure based on linked lists.
For random access get and set, ArrayList feels better than LinkedList, because LinkedList has to move the pointer.
For the add and delete operations add and remove, the LinedList is more dominant, because the ArrayList needs to move the data.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325014563&siteId=291194637