Add element at specific index in LinkedList in Java

In this tutorial, we will learn how to add element at specific index in LinkedList with in example.

LinkedList in Java is doubly-linked list implementation of the List and Deque interfaces. LinkedListclass extends AbstractSequentialList and implements the List, Deque, and Queue interfaces and permits null elements. It provides a linked-list data structure.

LinkedList class in Java provides a method add(int index, E element) to add element at specific index in LinkedList. This method inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

The syntax of add(int index, E element) method is:

index – index at which the specified element is to be inserted.
element – element to be inserted.

IndexOutOfBoundsException – if the index is out of range (index < 0 or index > size())




Related posts

Leave a Comment