LinkedList - add, add element at specific index methods program in java






LinkedList - add, add element at specific index methods program.




import java.util.LinkedList;
import java.util.List;
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
*/
public class LinkedListAddExample {
   public static void main(String args[]) {
          List<String> linkedList = new LinkedList<String>();
          System.out.println("--------add element to end of list");
          linkedList.add("ankit");
          linkedList.add("mittal");
          linkedList.add("javaMadeSoEasy");
          System.out.println("linkedList = "+linkedList);
          System.out.println("--------add element at 1st index i.e. after ankit");
          linkedList.add(1,"java");
          System.out.println("linkedList = "+linkedList);
         
   }
}
/*OUTPUT
--------add element to end of list
linkedList = [ankit, mittal, javaMadeSoEasy]
--------add element at 1st index i.e. after ankit
linkedList = [ankit, java, mittal, javaMadeSoEasy]
*/


RELATED LINKS>


ArrayList - remove, get, contains and set methods program


ArrayList - iterate using iterator, listIterator, Enumeration and enhanced for loop


ArrayList - fail-safe or fail-fast iteration using iterator, listIterator, Enumeration and enhanced for loop


ArrayList - synchronizing using Collections.synchronizedList


ArrayList - making list unmodifiable using Collections.unmodifiableList


Vector - add, add element at specific index methods program


eEdit
Must read for you :