Read Also : Difference between Array and ArrayList in Java
Difference between Linked List and Array in Java
1. Size : Array is static in size. You need to define the size of Array at the time of declaration. The size of the Array once defined can not be changed.Size of Linked List is dynamic in nature. You can add as many elements as you want to the Linked List unless memory becomes a constraint.
2. Memory Used : For storing the same number of elements, Array requires less memory than Linked List. Array only stores data[element] where as Linked List stores data [element] as well as the next node address.
3. Memory Allocation : Another difference between Linked List and Array is memory allocation.
An Array requires contiguous memory allocation, where as Linked List elements are present all over the heap memory. There is no limitation of contiguous memory in the case of Linked List.
4. Performance : Performance helps us to understand when to use Array over Linked List or when to use Linked List over Array.
Linked List gives O[n] performance for searching an element while Array gives O[1] performance for searching an element given you know the index.
Linked List is best suited for insertion and deletion operations. If you insert or delete the element at head or tail then the performance is O[1], otherwise the performance is O[n] , whereas Array gives O[n] performance for inserting or deleting an element.
5. Multi-Dimensional : The another difference between Linked List and Array is dimension. Array can be multi-dimensional in java.
Due to multi-dimensional nature of Array, it can be useful to represent matrices, 2d plain , 2d game etc.
Linked List is always single-dimensional but it is of two types, single linked list and double linked list.
6. Length : Length of the Linked List can be provided by the size[] method where as Array has the length variable which returns the length of the Array.
for example :
Integer arrayObject[] = new Integer[10];
arraylength= arrayObject.length ; //uses arrayobject length variable
arraylength= arrayObject.length ; //uses arrayobject length variable
LinkedList linkedlistObject = new LinkedList[];
linkedlistObject.add[12];
linkedlistObject.size[]; //uses linkedlistObject size method
linkedlistObject.add[12];
linkedlistObject.size[]; //uses linkedlistObject size method
7. Adding elements : You can insert the elements into the Linked List object using add[] method where as Array uses assignment operator[=] to insert the elements.
Integer addarrayobject[] = new Integer[3];
addarrayobject[0]= new Integer[8] ; //new object is added to the array object
addarrayobject[0]= new Integer[8] ; //new object is added to the array object
Example of Linked List and Array
import java.util.LinkedList;
import java.util.Iterator;
public class ArrayLinkedListExample {
public static void main[String[] args] {
// LinkedList Example
LinkedList linkedlistobj = new LinkedList[];
linkedlistobj.add["Alive is awesome"];
linkedlistobj.add["Love yourself"];
Iterator it = linkedlistobj.iterator[];
System.out.println["Linked List object output :"];
while[it.hasNext[]]
System.out.print[it.next[] + " "];
// Array Example
String[] arrayobj = new String[3];
arrayobj[0]= "Love yourself";
arrayobj[1]= "Alive is awesome";
arrayobj[2]= "Be in Present";
System.out.println[""];
System.out.println["Array object output :"];
for[int i=0; i