In: Computer Science
What are the advantages of a using a linked list rather than a conventional array in Java? and when would it be more efficient to use an array rather than a linked list? Explain your answer.
-> Linked list is a collection of data elements of same data type. it consists of two parts, one stores the data element and the other stores the address of the next element in the list.
-> Array is also the collection of data elements of the similar data type. unlike list, it stores only the data element.
Advantages of linked list over conventional array:
Linked list | conventional array |
linked list has dynamic memory allocation, any number of elements are added easily. | array has a contiguous memory allocation, so the insertion of the elements will be difficult |
linked lists are dynamic and flexible and can expand and contract its size easily. | arrays are fixed in size.and if people who use arrays has to know the number of elements they are going to store priorly |
operations like insertion , deletion are very fast. | opeartions like insertion , deletions consumes a lot of time |
linked list uses pointers to access the elements. hence using pointers the access will be very fast | array uses index to access elements. accessing is slow compared to linked list. |
-> when to use an array
Therefore , each has its own advantages and disadvantages , you have to make use of them according to the requirement.