AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-collection: What is the difference between ArrayList and LinkedList?

Please explain the key differences between ArrayList and LinkedList.

java x 211
collection x 52
Posted On : 2014-04-27 13:21:14.0
profile Garima Gupta - anyforum.in Garima Gupta
596129558962
up-rate
5
down-rate

Answers


ArrayList is better for adding and removing elements from the end of the container. All of the operations can be performed in constant time(i.e. time complexity) -- O(1).
Adding and removing elements from any other position is proven to be more expensive -- linear to be exact: O(n-i), where n refers the number of elements and i refers the index of the element to be added or removed.

For Example : ArrayList Has 6 elements and you want to add an element on 3rd position. Then ArrayList add the element on 3rd position and shifted existing 3rd position element to 4th, 4th position element to 5th, and so on.

LinkedList can add and remove an element at any position in constant time -- O(1). Indexing an element is a bit slower -- O(i) where i is the index of the element in case of LinkedList.

---------------------------------------------------------------------------------------------------------------------------------

Traversing ArrayList is also easier since you can simply use an index instead of having to create an iterator.
The LinkedList creates an internal object for each element inserted. So you have to be careful of the extra garbage being created.

---------------------------------------------------------------------------------------------------------------------------------

An ArrayList is a List implementation backed by a Java array. With a LinkedList, the List implementation is backed by a doubly linked list data structure.

---------------------------------------------------------------------------------------------------------------------------------

ArrayList stores objects in an array internally so it is fast in case of random access, and to insert a new element at a particular index it have to move subsequent elements one by one, hence it is slower while inserting and deleting.

LinkedList stores objects using nodes, each node has the address of next node, hence random access is slow and updation (i.e insertion and deletion) is faster in case of LinkedList.

Posted On : 2014-04-27 14:22:08
Satisfied : 1 Yes  0 No
profile Saksham Kumar - anyforum.in Saksham Kumar
73433939092
Reply This Thread
up-rate
5
down-rate

ArrayList and LinkedList are the Collection classes , and both of them implements the List interface. LinkedList implements it with a doubly-linked list while ArrayList implements it with a dynamically re-sizing array.

Search Operation
Manipulation
Behaviour
Memory Overhead

More....

http://net-informations.com/java/cjava/linkedlist.htm


Posted On : 2017-07-04 11:22:27
Satisfied : 1 Yes  0 No
profile rahul kumar - anyforum.in rahul kumar
050
Reply This Thread
up-rate
3
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in