This Java Example shows how to get an element from first and last position in java LinkedList object.
Get Element From First and Last Position Of Java LinkedList Example
This Java Example shows how to get an element from first and last
position in java LinkedList object. To get an element from first and last position of LinkedList We use void
getFirst() And getLast() method. These method returns the specified
element at the First and last position of the LinkedList respectively.
Example: GetFirstandLastElement.java
package devmanuals.com; import java.util.*; public class GetFirstandLastElement { public static void main(String[] args) { LinkedList LKLST = new LinkedList(); LKLST.add("1"); LKLST.add("2"); LKLST.add("3"); LKLST.add("4"); LKLST.add("5"); System.out.println("The Linked List is as: "+LKLST); System.out.println("First Element : " + LKLST.getFirst()); System.out.println("Last Element : " + LKLST.getLast()); } }
Output:
The Linked List is as: [1, 2, 3, 4, 5]
First Element : 1 Last Element : 5 |
[ 0 ] Comments