Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinkedList does not provide index based access, so why does it have get(index) method?

I understand that ArrayList is index based datastructure, that allows you to access its element using the index but LinkedList is not supposed index based so why does it have get(index) method that allows direct access to the element?

like image 924
user3342469 Avatar asked Feb 23 '14 04:02

user3342469


1 Answers

It may not be efficient to retrieve items from a linked list by index, but linked lists do have indices, and sometimes you just need to retrieve an item at a certain index. When that happens, it's much better to have a get method than to force users to grab an iterator and iterate to the desired position. As long as you don't call it too much or the list is small, it's fine.

like image 132
user2357112 supports Monica Avatar answered Sep 28 '22 05:09

user2357112 supports Monica