Add An Element To Specified Index Of Java LinkedList

Add An Element To Specified Index Of Java LinkedList


Posted in : Java Posted on : November 29, 2010 at 5:07 PM Comments : [ 0 ]

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]

Download The Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics