Java Collection Framework collection (2) ArrayList and LinkedList

Source: Internet
Author: User

ArrayList is a list based on a dynamic array implementation, and LinkedList is a list based on a linked List. so, ArrayList has an array of features, and LinkedList has the characteristics of the List.

    • Advantages and Disadvantages

ArrayList

Advantage: because the array is indexed (index) based data structure, suitable for random reading of data, read fast, can be a step get (index).

Cons: Add Delete value is very slow, on the one hand, adding data in the middle of the array, you need to move the following number, on the other hand, when the length is greater than the initial length, each add a number, will need to expand.

Linkedlist: doubly linked list

Advantages: Add Delete value quickly, do not need to change the size of the array, do not need to reload all the data in the array to a new array, add in the middle of the list also only need to change the pointer, the length is not fixed, the advantage exists only in the data inserted into the table header, if the back inserted, there is no advantage.

LinkedList is better than ArrayList to implement stacks and queues.

Disadvantage: 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.

    • Other

The time complexity of the linkedlist of the Remove (int) and remove (Object) is O (n), not o (1). because there is a process for finding it.

The LinkedList remove (int) is better than remove (Object), because remove (int) looks for it from the middle of the list, and if int is smaller than the middle, look for the first half, otherwise look for the second half (similar to binary search).

The addition of ArrayList is more expensive than the linkedlist, because there is also the movement of data except for the time complexity of finding it.

 packagecom.company;Importjava.util.ArrayList;Importjava.util.LinkedList;Importjava.util.List;/*** Created by wangbin10 on 2017/3/8.*/ public classArraylinklist { public Static voidmain (String [] Args) {System.out.println ("ArrayList add:" +timelist (NewArrayList ())); System.out.println ("linkedlist add:" +timelist (NewLinkedList ())); List List1=addlist (NewArrayList ()); List List2=addlist (NewLinkedList ()); System.out.println ("ArrayList read:" +readlist (list1)); System.out.println ("linkedlist read:" +readlist (list2)); }    Static Final intn=100000; Static Longtimelist (list List) {Longstart=System.currenttimemillis (); Object o=NewObject ();  for(inti=0;i<n;i++) {list.add (i,o); }    returnSystem.currenttimemillis ()-start; }    Static Longreadlist (list List) {Longstart=System.currenttimemillis ();  for(intI=0;i<list.size (); i++){        }        returnSystem.currenttimemillis ()-start; }    Staticlist addlist (list list) {Object o=NewObject ();  for(inti=0;i<n;i++) {list.add (i,o); }        returnlist; }}

Code output:

ArrayList Add:3
LinkedList Add:7
ArrayList read:1
LinkedList read:1

Java Collection Framework collection (2) ArrayList and 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.