Search This Blog

Thursday 14 August 2014

Linked List - JAVA

Linked List in Java

Array List is faster than Linked List but you can only add at the end in the array list. Linked List allows you to add anywhere in the list.


This program shows how to use Linked List in Java.

LinkedList.java

import java.util.LinkedList;

public class MainClass {
 public static void main(String args[]){
  LinkedList linkedList = new LinkedList();
  linkedList.add("Item 1");
  linkedList.add("Item 2");
  System.out.println(linkedList);
  
  //I want to add an item in between item 1 and item 2
  System.out.println("Adding Item in between item 1 & item 2");
  linkedList.add(1, "Item 1.5"); //at index 1
  System.out.println(linkedList);
 }
}

No comments:

Post a Comment