Arrays/Linked List

When and on what conditions will you decide whether to use linked lists or arrays, also Which process is fastest and which is slowest in arrays as well as linked list i.e, sorting, searching, add, delete etc.

Questions by bhagyashree010   answers by bhagyashree010

Editorial / Best Answer

Aqan12  

  • Member Since Apr-2010 | Jun 13th, 2010


Memory:
When you create an array you allocate all the memory while initializing the array, so if you had an array of 1000 Objects, you'd allocate space to hold 1000 object references as soon as you initialize the array.

LinkedList on the other hand extends dynamically as the objects are added to the linked list.

Accessing Elements:
Arrays are useful if you are going to access elements using the index. if you need to access the 100th element you can directly access it by a[100].

To access 100th element in a LinkedList you'll have to traverse 99 elements to reach the 100th element.

LinkedList can handle addition/deletion of elements better then arrays and that's one of the most powerful feature of LL over arrays.

Adding/Inserting Elements:
Array is fixed size; to add a number of elements greater then the size of the array you'd have to copy the array into a new bigger array.

To insert an element in the middle of an array all the elements after the inserted element would have to moved up one index.

LinkedList can grow dynamically.

Deleting Elements:
To delete an element from Array you'd have to move down all the elements after the deleted element.

LinkedList elements can be deleted dynamically.

To summarize, LinkedList can handle manipulation of data structure much better and efficiently manage memory but arrays have better accessibility when accessing elements by index.
Search and Sort would be similar performance on both.

Showing Answers 1 - 21 of 21 Answers

We select Arrays when the size of the array is fixed.
We select Lists when we have to add objects dynamically where the size of the list would vary at runtime.

Sort operation is fastest in arrays.
Addition, deletion of items at a particular location is fastest in lists.

bharath.334

  • Jun 10th, 2010
 

Prefer arrays if you know the size and using arrays is the best practice and is more efficient then others.

And when it comes to ArrayList or LinkedList, use arrayList when you have more retrieval of the elements. I mean insertion is not much affective in arrayList. Use LinkedList when you have more insertion operation than retrieval.

Insertion in Arraylist is not advised because if you insert an element/object in the middle of a list then all the elements in the list towards right are right shifted. Since it may affect performance.

Memory:
When you create an array you allocate all the memory while initializing the array, so if you had an array of 1000 Objects, you'd allocate space to hold 1000 object references as soon as you initialize the array.

LinkedList on the other hand extends dynamically as the objects are added to the linked list.

Accessing Elements:
Arrays are useful if you are going to access elements using the index. if you need to access the 100th element you can directly access it by a[100].

To access 100th element in a LinkedList you'll have to traverse 99 elements to reach the 100th element.

LinkedList can handle addition/deletion of elements better then arrays and that's one of the most powerful feature of LL over arrays.

Adding/Inserting Elements:
Array is fixed size; to add a number of elements greater then the size of the array you'd have to copy the array into a new bigger array.

To insert an element in the middle of an array all the elements after the inserted element would have to moved up one index.

LinkedList can grow dynamically.

Deleting Elements:
To delete an element from Array you'd have to move down all the elements after the deleted element.

LinkedList elements can be deleted dynamically.

To summarize, LinkedList can handle manipulation of data structure much better and efficiently manage memory but arrays have better accessibility when accessing elements by index.
Search and Sort would be similar performance on both.

a) A LinkedList is similar to an ArrayList in that it is ordered by index position, but it differs in that the elements are double-linked to one another. This linkage gives you new mehtods for adding and removing from the beginning or end.

b)Random access is faster with ArrayList than LinkedList

c)Insertion and deletion in the middle is faster with LinkedList than ArrayList

Anubhavs

  • Aug 6th, 2010
 

Linked list is best under the feature of the manipulation e.g. insertion, deletion etc. In the other way the array will go through a good way if we focus on the accessibility.

  Was this answer useful?  Yes

In all way Linked list is best then Arrays, since its a next level of Array

1. Linked list is dynamic allocation where as Arrays are not
2. We can get and set the value with the use of get, set methods
3. Using List Iterator we can traverse both the sides (not in Array)
4. Many methods available to manipulate Linked List.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions