LinkedList E removeLast() method Example Program


Removes and returns the last element from this list.

Program

package com.candidjava;

import java.util.LinkedList;

/**
 * 
 * @author : mohan
 * @description :The java.util.LinkedList.removeLast() method removes and
 *              returns the last element from this list.
 */
public class LinkedListRemovLast {
	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("Last element:" + list.removeLast());
		System.out.println("LinkedList:" + list);
	}
}

Output

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

Explanation

public E removeLast()
Removes and returns the last element from this list.

Specified by:
removeLast in interface Deque<E>

Returns:
the last element from this list

Throws:
NoSuchElementException - if this list is empty


Related Post

Comments


©candidjava.com