Web | This Site

Java Basics Examples
Java Tutorial
Java Useful Resources
Selected Reading
© 2011 TutorialsPoint.COM

| Home |  | References |  | Discussion Forums |  | About TP

# Java Examples - First and last element

## Problem Description:

How to get the first and the last element of a linked list ?

## Solution:

Following example shows how to get the first and last element of a linked list with the help of linkedlistname.getFirst() and linkedlistname.getLast() of LinkedList class.

    import java.util.LinkedList;

    public class Main {
       public static void main(String[] args) {
          LinkedList lList = new LinkedList();
          lList.add("100");
          lList.add("200");
          lList.add("300");
          lList.add("400");
          lList.add("500");
          System.out.println("First element of LinkedList is :
          " + lList.getFirst());
          System.out.println("Last element of LinkedList is :
          " + lList.getLast());
       }
    }

## Result:

The above code sample will produce the following result.

    First element of LinkedList is :100
    Last element of LinkedList is :500

Advertisement

Online Image Processing

Indian Baby Names