0

I have found following question on www.javatpoint.com
If you were to use a List implementation,but not sure which one to, because the requirement is not yet clear. In this case which List implementation will you use ?
options: 1. ArrayList 2. LinkedList

Correct answer for this is ArrayList
But there is no explanation why, Please help me to uderstand

Gan
  • 624
  • 1
  • 10
  • 31
  • 1
    The question is wrong. Don’t worry about it. – Ry- May 31 '17 at 04:14
  • 4
    Possible duplicate of [When to use LinkedList over ArrayList?](https://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist) – Sergey May 31 '17 at 04:16
  • Although I'm also curious about the arguments they would use to declare that ArrayList should be the default, check out [this answer](https://stackoverflow.com/a/322742/3586783) for a good side-by-side comparison of the two list implementations. – pacifier21 May 31 '17 at 04:16
  • 1
    Maybe because `LinkedList` has only few advantages in very special cases.There are even discussions to deprecate it. – Henry May 31 '17 at 04:17
  • https://docs.oracle.com/javase/7/docs/api/java/util/package-tree.html – Blasanka May 31 '17 at 04:23

4 Answers4

0

One possible reason might be that elements in ArrayList consume less memory space than in LinkedList, because each element in LinkedList contains a value plus a pointer to the next element, while an element in ArrayList has only value

Y.Liu
  • 54
  • 1
  • 7
0

ArrayList stores elements in array in insert order. You can get value by its index in array.

LinkedList stores your elements in Node objects that references each other. Each Node references previous and next Node. You can get elements sequentially starting from both sides. You can also get elements by index but it's not as fast as in ArrayList. Use LinkedList when you need sequential access, like queue or stack data structure

Jay Smith
  • 2,331
  • 3
  • 16
  • 27
0

From what I've read in the past: An ArrayList essentially acts as a better array, allowing for dynamic resizing. A LinkedList is a double linked list which causes it to have better performance on adding and removing items from the list, but getting and setting at arbitrary positions is slower than an ArrayList. So, the question is most likely assuming that, generally, a program will be getting and setting values more than it is adding and removing values, which is a fairly reasonable assumption, but the question in question is incorrect.

Greg S.
  • 48
  • 8
  • Clarification: getting and setting _at an arbitrary position in the list_ is slower than an `ArrayList`. If the only thing you ever get is the first or last element, there should be no difference in performance. – ajb May 31 '17 at 04:47
  • Thank you. Clarification added. – Greg S. May 31 '17 at 05:23
0

Simple Term

ArrayList : Iterating Over An ArrayList is faster than Linked List, Because All Elements Stored in Contiguous Memory Location. But Performing Operation Like Delete Will Reduce Performance Because Again Entire List Order Changes (Like if you delete element at position 3rd then all next elements location are currentLocation - 1).

LinkedList : Slower When Iteration Performed(As Compare To ArrayList). But Delete and Update Operation Becomes Faster Because If you delete any element at any position only previous and after element locations are changed(Not Entire List).

So When You Don't Have Clear Requirements Just Iteration Is Basic Need (And Array List Gives Best Performance).

Abhishek
  • 3,348
  • 3
  • 15
  • 34