Difference Between Similar Terms and Objects

Difference between Array List and Linked List

Difference Between Array List and Linked List

How Is Data Stored?

Array list and Linked list are common terms when it comes to data storage and retrieval. Though there are lots of storage devices, ultimately, they depend on the storage mechanism. These two storage mechanisms place your data in the storage devices and retrieve them when needed. Let’s take a look at how they store data in their memory. The Array list uses a sequential storage, and the pieces of data are stored one after the other. This is perhaps a simpler form of storage – it avoids confusion. Yes, we can retrieve the next item or data from the next memory location of the array list; however, it is stored with the help of pointers in the Linked list. Here we need two memory locations for storage – one for the data, the other for the pointer. A pointer addresses the memory location of the next data. We can easily understand that Linked list never stores data sequentially; rather, it uses a random storage mechanism. The pointers are the key elements in locating the data locations in the memory.

Dynamic Array and Linked List

We have already discussed how both storage mechanisms put in data and we can give a term ‘dynamic array’ for the Array list’s internal storage scheme. It just puts data pieces one after the other –hence the name – whereas the Linked list uses an internal list with the help of pointers to track the next item. Therefore, it uses an internal linked list, like a singly or doubly linked list to show us the next data.

Memory Usage

As Array list stores only the actual data, we need space only for the data we store. Conversely, in the Linked list, we use pointers as well. Therefore, two memory locations are required, and we can say that linked list consumes more memory than the Array list. An advantageous side of Linked list is that it never requires continuous memory locations to store our data, as opposed to the Array list. The pointers are capable of holding the position of the next data location, and we can even use smaller memory slots that are not continuous. When it comes to memory usage, pointers play the main role in the Linked list, and so does the effectiveness of them.

Size of Initial Array List and Linked List

With the Array list, even an empty list requires a size of 10, but with a Linked list, we do not need such a huge space. We can create an empty Linked list with a size of 0. Later on, we can increase the size as needed.

Data Retrieval

Data retrieval is simpler in Array list as it stores sequentially. All it does is identify the first data location; from there, the next location is accessed sequentially to retrieve the rest. It computes like the first data position + ‘n’, where ‘n’ is the order of the data in the Array list. The Linked list refers the initial pointer to find the first data location, and from there it refers the pointer associated with each data to find the next data location. The retrieval process is mainly dependent on the pointers here, and they effectively show us the next data location.

End of Data

The Array list uses a null value to mark the end of the data, whereas the Linked list uses a null pointer for this purpose. As soon as the system recognizes null data, the Array list stops the next data retrieval. In a similar manner, the null pointer stops the system from proceeding to the next data retrieval.

Reverse Traversal

The Linked list allows us to traverse in the reverse directions with the help of descendingiterator(). However, we do not have such a facility in an Array list – reverse traversal becomes a problem here.

Syntax

Let us look at the Java Syntax of both storage mechanisms.

Array list creation:

List <String> arraylistsample = new ArrayList <String> ();

Adding objects to the Array List:

Arraylistsample.add(“name1”);

Arraylistsample.add(“name2”);

This is how the resultant Array list will look like – [name1, name2].

Linked list creation:

                List <String> linkedlistsample = new linkedList <String> ();

Adding objects to the Linked List:

                Linkedlistsample.add(“name3”);

Linkedlistsample.add(“name4”);

This is how the resultant Linked list will look like – [name3, name4].

 Which Is Better for the Get or Search Operation?

The Array list takes O(1) time to run any data search, whereas the Linked list takes u O(n) for the nth data search. Therefore, an Array list always uses a constant time for any data search, but in the Linked list, the time taken depends on the position of the data. Therefore, Array lists are always a better choice for Get or Search operations.

Which Is better for the Insertion or Addition Operation?

Both the Array list and the Linked List take O(1) time for data addition. But if the array is full, then the Array list takes a considerable amount of time to resize it and copy the items to the newer one. In such a case, the Linked list is the better choice.

Which Is Better for the Remove Operation?

The remove operation takes almost the same amount of time in both the Array list and the Linked list. In the Array list, this operation deletes the data and then shifts the position of the data to form the newer array – it takes O(n) time. In the Linked list, this operation traverses to the particular data and changes pointer positions to form the newer list. The time for the traversal and the removal is O(n) here as well.

Which Is Faster?

We know that an Array list uses an internal array to store the actual data. Therefore, if any data is deleted, then all the forthcoming data needs a memory shift. Obviously, this requires a considerable amount of time and slows things down. Such a memory shift is not required in the Linked list, as all it does is change the pointer location. Therefore, a Linked list is faster than an Array list in any kind of data storage. However, this is purely dependent on the type of operation, i.e. for the Get or Search operation, the Linked list takes a lot more time than an Array list. When we look at the overall performance, we can say that the Linked list is faster.

When to Use an Array List and a Linked List?                                                                                                           

An Array list is best suited for smaller data requirements where continuous memory is available. But when we deal with huge amounts of data, the availability of continuous memory implements the data storage mechanisms, whether it is small or huge. Next, decide which one to choose – the Array list or the Linked list. You can go ahead with an array list when you just need storage and retrieval of data. But a list can help you beyond that by manipulating data. Once you decide how frequently data manipulation is required, it is important to check what type of data retrieval you usually perform. When it is just Get or Search, then the Array List is the better choice; for other operations such as Insertion or Deletion, go ahead with the Linked list.

Let us look at the differences in tabular form.

S.No Concepts Differences
Array List Linked List
1 Data Storage Fashion Uses sequential data storage Uses non-sequential data storage
2 Internal Storage Scheme Maintains an internal Dynamic Array Maintains a Linked list
3 Memory Usage Requires memory space just for the data Requires memory space for data as well for pointers
4 Size of the Initial List Needs space for at least 10 items Does not need space and we can even create an empty Linked list of size 0.
5 Data Retrieval Computes like the first data position + ‘n’, where ‘n’ is the order of the data in the Array list Traversal from the first or last till the required data is required
6 End of Data The Null values mark the end The Null Pointer marks the end
7 Reverse Traversal Does not allow it Allows it with the help of descendingiterator()
8 List Creation Syntax List <String> arraylistsample = new ArrayList <String> ();

 

List <String> linkedlistsample = new linkedList <String> ();

 

9 Adding Objects Arraylistsample.add(“name1”);

 

Linkedlistsample.add(“name3”);

 

10 Get or Search Takes O(1) time and is better in performance Takes O(n) time and performance is dependent on the position of the data
11 Insert or Addition Consumes O(1) time except when the array is full Consumes O(1) time under all circumstances
12 Deletion or Removal Takes O(n) time Takes O(n) time
13 When to Use? When there are lots of Get or Search operations involved; the memory availability should be higher even at the start When there are lots of Insert or Delete operations, and the memory availability does not need to be continuous

Sharing is caring!


Search DifferenceBetween.net :




Email This Post Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.


1 Comment

  1. ArrayList and LinkedList are two popular collection classes in Java and Major difference between ArrayList and LinkedList is on there implementation.

Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

References :


[0]http://javahungry.blogspot.com/2015/04/difference-between-arraylist-and-linkedlist-in-java-example.html

[1]https://www.javatpoint.com/difference-between-arraylist-and-linkedlist

[2]http://www.techfaq360.com/viewFreshers.jsp?tutorialId=254

[3]https://stackoverflow.com/questions/21974361/what-java-collection-should-i-use

Articles on DifferenceBetween.net are general information, and are not intended to substitute for professional advice. The information is "AS IS", "WITH ALL FAULTS". User assumes all risk of use, damage, or injury. You agree that we have no liability for any damages.


See more about : ,
Protected by Copyscape Plagiarism Finder