Mega Code Archive

 
Categories / Java / Collections Data Structure
 

Use addFirst method to add value to the first position in a linked list

import java.util.LinkedList; public class MainClass {   public static void main(String[] a) {     LinkedList<String> officers = new LinkedList<String>();     officers.addFirst("B");     officers.addFirst("B");     officers.addFirst("H");     officers.addFirst("P");     officers.addFirst("M");     for (String s : officers)       System.out.println(s);   } }