-5

What is index in linked List of java because linked list is not a continuous memory ???

List l=new linkedList();
     l.get(index);
     l.add(index);
  1. what is index in linkedList of java because linked list is not allocate continuous memory
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
sarvoday
  • 1
  • 3
  • Even `ArrayList` is not guaranteed to be backed by a single chunk of memory. See http://stackoverflow.com/questions/10224888/java-are-1-d-arrays-always-contiguous-in-memory – zapl Jun 23 '16 at 10:05

1 Answers1

0

If you are referring to java.util.LinkedList, the index is simply the position of an element in the List. Any List implementation is an ordered Collection, so elements can be accessed via their index.

However, for LinkedList, methods such as get(index) are not efficient, since they have to traverse the List from its beginning or from its end (depending on which is closer to the required index), which takes linear time.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • thanks a lot Eran, One request to you. 1.why we need to maintain insertion order of any collection (array list,linked list and all linked collection) 2. Is there any benefit to maintain insertion order in real time – sarvoday Jun 24 '16 at 05:17
  • @sarvoday It all depends on the use case. Sometimes you need to maintain order (either insertion order or the order you get after sorting) and sometimes you don't. If you don't need to maintain order, and you don't want to allow duplicate elements, you can always use a Set instead of a List. – Eran Jun 24 '16 at 07:45