Get Element From First and Last Position Of Java LinkedList

Get Element From First and Last Position Of Java LinkedList


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

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

Get Element From First and Last Position Of Java LinkedList Example

This Java Example shows how to get an element from first and last  position in java LinkedList object. To get an element from first and last position of LinkedList We use void getFirst() And getLast() method. These method returns the specified element at the First and last position of the LinkedList respectively. 

 Example: GetFirstandLastElement.java

package devmanuals.com;

import java.util.*;

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

		LinkedList LKLST = new LinkedList();
		LKLST.add("1");
		LKLST.add("2");
		LKLST.add("3");
		LKLST.add("4");
		LKLST.add("5");
		System.out.println("The Linked List is as: "+LKLST);
		System.out.println("First Element : " + LKLST.getFirst());
		System.out.println("Last Element  : " + LKLST.getLast());

	}
}

Output:

The Linked List is as: [1, 2, 3, 4, 5] 

First Element : 1

 Last Element : 5

Download The Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics