LinkedList E pop() method Example Program


Pops an element from the stack represented by this list. In other words, removes and returns the first element of this list.
This method is equivalent to removeFirst().

Program

package com.candidjava;

import java.util.LinkedList;

/**
 * 
 * @author : mohan
 * @description :The java.util.LinkedList.pop() method pops an element from the
 *              stack represented by this list.Popping means remove and return
 *              the first element of this list.
 */
public class LinkedListPop {
	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("Pop element in the list:" + list.pop());
		System.out.println("LinkedList:" + list);
	}
}

Output

LinkedList:[anandh, hari, vinoth, mohan, kamal]
Pop element in the list:anandh
LinkedList:[hari, vinoth, mohan, kamal]

Explanation

public E pop()
Pops an element from the stack represented by this list. In other words, removes and returns the first element of this list.
This method is equivalent to removeFirst().

Specified by:
pop in interface Deque<E>

Returns:
the element at the front of this list (which is the top of the stack represented by this list)

Throws:
NoSuchElementException - if this list is empty

Since:
1.6


Related Post

Comments


©candidjava.com