This Java Example shows how to add an element at first and last position in java LinkedList object.
Add Element To First and Last Position Of Java LinkedList Example
This Java Example shows how to add an element at first and last
position in java LinkedList object. To
add an element at first and last position of LinkedList We use void
addFirst(Object obj) And addLast(Object obj) method. These method appends the specified
element at the First and last position in the LinkedList respectively.
Example: AddFirstAndAddLastElement.java
package devmanuals.com; import java.util.*; public class AddFirstAndAddLastElement { public static void main(String[] args) { LinkedList LKLST = new LinkedList(); LKLST.add("2"); LKLST.add("3"); LKLST.add("4"); System.out.println("Before addition : " + LKLST); LKLST.addFirst("1"); LKLST.addLast("5"); System.out.println("After addition : " + LKLST); } }
Output:
Before addition : [2, 3, 4]
After addition : [1, 2, 3, 4, 5] |
[ 0 ] Comments