LinkedList E getFirst()method Example Program


Returns the first element in this list.

Program

package com.candidjava;

import java.util.LinkedList;

/**
 * 
 * @author : mohan
 * @description :The java.util.LinkedList.getFirst() method returns the first
 *              element in this list.
 */
public class LinkedListGetFirst {
	public static void main(String[] args) {
		LinkedList list = new LinkedList();
		list.add("anandh");
		list.add("hari");
		list.add("vinoth");
		list.add("mohan");
		System.out.println("LinkedList:" + list);
		System.out.println("First Element :" + list.getFirst());
	}
}

Output

LinkedList:[anandh, hari, vinoth, mohan]
First Element :anandh

Explanation

public E getFirst()
Returns the first element in this list.
Specified by:
getFirst in interface Deque<E>
Returns:
the first element in this list
Throws:
NoSuchElementException - if this list is empty


Related Post

Comments


©candidjava.com