Junior — Middle
Is it possible to call the get method on a LinkedList with an index position?
sobes.tech AI
Answer from AI
Yes, in Java, the LinkedList class has a get(int index) method that allows you to retrieve an element at a specified index. However, it's important to remember that LinkedList is implemented as a doubly linked list, so access by index is not as fast as in ArrayList — the access time is linear (O(n)).
Example usage:
LinkedList<String> list = new LinkedList<>();
list.add("First");
list.add("Second");
String element = list.get(1); // Returns "Second"
If you often need quick access by index, it's better to use ArrayList.