LinkedList E element() method Example Program


Retrieves, but does not remove, the head (first element) of this list.

Program

package com.candidjava;

import java.util.LinkedList;

/**
 * 
 * @author : mohan
 * @description :The java.util.LinkedList.element() method retrieves, but does
 *              not remove, the head (first element) of this list.
 */
public class LinkedListElement {

	public static void main(String[] args) {

		LinkedList list = new LinkedList();

		list.add("vinoth");
		list.add("kamal");
		list.add("karthi");
		list.add("anand");

		System.out.println("LinkedList:" + list);

		System.out.println("Head of list:" + list.element());
	}
}

Output

LinkedList:[vinoth, kamal, karthi, anand]
Head of list:vinoth

Explanation

public E element()
Retrieves, but does not remove, the head (first element) of this list.

Specified by:
element in interface Deque<E>

Specified by:
element in interface Queue<E>

Returns:
the head of this list

Throws:
NoSuchElementException - if this list is empty

Since:
1.5


Related Post

Comments


©candidjava.com