Mega Code Archive

 
Categories / Java / Collections Data Structure
 

To insert an object into a specific position into the list, specify the index in the add method

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