Add Element To First and Last Position Of Java LinkedList

Add Element To First and Last Position Of Java LinkedList


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

This Java Example shows how to add an element at first and last position in java LinkedList object.

Add Element To First and Last Position Of Java LinkedList Example

This Java Example shows how to add an element at first and last  position in java LinkedList object. To add an element at first and last position of LinkedList We use void addFirst(Object obj) And addLast(Object obj) method. These method appends the specified element at the First and last position in the LinkedList respectively. 

 Example: AddFirstAndAddLastElement.java

package devmanuals.com;

import java.util.*;

public class AddFirstAndAddLastElement {
	public static void main(String[] args) {

		LinkedList LKLST = new LinkedList();
		LKLST.add("2");
		LKLST.add("3");
		LKLST.add("4");
		System.out.println("Before addition : " + LKLST);
		LKLST.addFirst("1");
		LKLST.addLast("5");
		System.out.println("After addition  : " + LKLST);

	}
}

Output:

Before addition : [2, 3, 4] 

After addition : [1, 2, 3, 4, 5]

Download The Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics