This Java Example shows how to add an element at specified index of java LinkedList.
Add An Element To Specified Index Of Java LinkedList Example
This Java Example shows how to add an element at specified index of java LinkedList. To
add an element at the specified index of LinkedList, we use void add(int index, Object obj) method. This method inserts the specified
element at the specified index in the LinkedList.
Example: AddElementAtLinkedListIndex.java
package devmanuals.com; import java.util.*; public class AddElementAtLinkedListIndex { public static void main(String[] args) { LinkedList LKLST = new LinkedList(); LKLST.add("Gyan"); LKLST.add("Singh"); LKLST.add("Arunesh"); LKLST.add("Kumar"); System.out.println("Before addition : " + LKLST); LKLST.add(2, "Gangwar"); LKLST.add(5, "Kushwaha"); System.out.println("After addition : " + LKLST); } }
Output:
Before addition : [Gyan, Singh, Arunesh, Kumar]
After addition : [Gyan, Singh, Gangwar, Arunesh, Kumar, Kushwaha] |
[ 0 ] Comments