LinkedList E removeFirst() method Example Program


Removes and returns the first element from this list.

Program

package com.candidjava;

import java.util.LinkedList;

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

Output

LinkedList:[anandh, hari, vinoth, mohan, kamal]
First element:anandh
LinkedList:[hari, vinoth, mohan, kamal]

Explanation

public E removeFirst()
Removes and returns the first element from this list.

Specified by:
removeFirst in interface Deque<E>

Returns:
the first element from this list

Throws:
NoSuchElementException - if this list is empty


Related Post

Comments


©candidjava.com