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>