ArrayList and LinkedList differences

Source: Internet
Author: User

ArrayList and LinkedList differences:

In a nutshell: ArrayList through the array form to store the management object, LinkedList is through the linked list way.


Common:

Linkedelist and ArrayList both implement the list interface

ArrayList and LinkedList are two collection classes that store a series of object references (references).

But they do not work the same way.

The main difference between them is that ArrayList is a resizable array, and LinkedList is a two-way link string (doubly LinkedList).

ArrayList is more popular and ArrayList is more suitable than linkedlist in many scenarios.

Difference:

The time taken for random access by the ArrayList is fixed , so it is more efficient at random access.

LinkedList does not support fast random interviews , but is more efficient when inserting deletes.


Because ArrayList's internal implementation is based on an array of objects, it uses the Get method to access any element in the list (random access), which is faster than LinkedList.

The Get method in LinkedList is checked from one end of the list in order until the other end. Therefore, the ArrayList efficiency of random access is significantly higher than that of LinkedList.


However, linkedlist efficiency is significantly higher than ArrayList in a large number of insert and delete operations.


Here is the case when inserting data at the beginning and end.

1.ArrayList is the realization of the data structure based on dynamic array, LinkedList data structure based on linked list.
2. For random access get and set,arraylist feel better than LinkedList, because linkedlist to move the pointer.
3. Add and Remove,linedlist are the dominant for new and deleted operations because ArrayList is moving the data.
This point depends on the actual situation.

If you insert or delete only a single piece of data, the ArrayList speed is better than LinkedList.

But if bulk random insert deletes data, LinkedList speed is much better than ArrayList. Because ArrayList each insertion of data, you move the insertion point and all subsequent data.

I did the experiment on this point. In the first 200,000 "records" of ArrayList and LinkedList insert 20,000 data, LinkedList time is about ArrayList 20 1.


When an element is added to the beginning of ArrayList, all elements that already exist are moved back, meaning that the overhead of data movement and replication is less efficient.

Conversely, adding an element to the beginning of LinkedList is simply not an element that allocates a record and then adjusts two connections. so the efficiency is higher.


The overhead of adding an element at the beginning of the LinkedList is fixed, while the overhead of adding an element at the beginning of ArrayList is proportional to the size of the ArrayList.


1. For ArrayList and LinkedList, the overhead of adding an element at the end of the list is fixed. For ArrayList, the main point is to add an entry in the internal array, pointing to the element being added, which may occasionally cause the array to be redistributed, whereas for LinkedList, the overhead is uniform, allocating an internal entry object.

2. Inserting or deleting an element in the middle of a ArrayList means that the remaining elements in the list will be moved, while the overhead of inserting or deleting an element in the middle of the linkedlist is fixed.

3. LinkedList does not support efficient random element access.

4. ArrayList space waste is mainly reflected in the end of the list to reserve a certain amount of space, and the space cost of LinkedList is reflected in its every element needs to consume considerable space

It can be said that using ArrayList provides better performance when the action is to add data after a column of data rather than in front or in the middle, and to randomly access its elements, and when your action is to add or delete data in front or in the middle of a column of data, and to access its elements sequentially, You should use the LinkedList.


the difference between LinkedList and ArrayList comes mainly from the differences between the array and the LINKEDLIST data structures. If you are familiar with arrays and linkedlist, you can easily conclude that:


1) Because array is an index-based data structure, it is very fast to use an index to search for and read data in an array. The time complexity of getting data in an array is O (1), but deleting the data is expensive because it requires all the data in the array to be re-queued.


2) The insertion is faster than for arraylist,linkedlist.

Because LinkedList does not need to change the size of the array like ArrayList, it does not need to reload all the data into a new array when the array is full, which is the worst case of ArrayList, the time complexity is O (n), The time complexity of insertion or deletion in LinkedList is only O (1).

ArrayList also needs to update the index when inserting data (in addition to inserting the tail of the array).


3) similar to inserting data, when deleting data, LinkedList is also better than ArrayList.


4) LinkedList requires more memory because the location of each index of ArrayList is the actual data, and each node in LinkedList stores the actual data and the location of the front and back nodes.


What scenario is more appropriate to use LinkedList instead of ArrayList


As I mentioned earlier, ArrayList is more popular in many scenarios, but in some cases linkedlist is more appropriate. Such as:


1) Your app does not randomly access the data. Because if you need the nth element in LinkedList, you need to count from the first element to the nth data and then read the data.


2) Your app more insert and delete elements, less read data. Because inserting and deleting elements does not involve rearrangement of data, it is faster than ArrayList.


These are the differences between ArrayList and LinkedList. When you need an out-of-sync index-based data access, use ArrayList as much as possible. ArrayList is quick and easy to use. But remember to give the appropriate initial size, as much as possible to reduce the size of the change array.

Summarize:

1. ArrayList is a data structure based on dynamic array, LinkedList data structure based on linked list.

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

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


The difference between ArrayList and LinkedList is repeated more than once, if you can not understand it after reading it is not justified. Oh




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ArrayList and LinkedList differences

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.