Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Link List Example in Java

 
 

Share on Google+Share on Google+

Link List Example in Java

Advertisement
In this example, we are going to show the use of java.util.LinkedList class.

Link List Example in Java

     

In this example, we are going to show the use of java.util.LinkedList class. You will be creating an object of link list class and performing various operation like adding and removing object.  

This class extends AbstractSequentialList and implements List, Cloneable, Serializable. It permits all elements including null. LinkedList class provides  methods get, insert and remove  an element at the beginning and end of the list.

In this example we are using six methods of LinkedList class.

add(Object o): Appends the specified element to the end of this list. It returns a boolean value.

size(): Returns the number of elements in this list.

addFirst(Object o): Inserts the given element at the beginning of this list.

addLast(Object o): Inserts the given element at the last of this list.

add(int index,Object o): Insert the specified element at the specified position in this list. It throws IndexOutOfBoundsException if index is out of range. 

remove(int index): Remove the element at the specified position in this list. It returns the element that was removed from the list. It throws IndexOutOfBoundsException if index is out of range. 

The code of the program is given below:

import java.util.*;

public class LinkedListDemo{
  public static void main(String[] args){
  LinkedList link=new LinkedList();
  link.add("a");
  link.add("b");
  link.add(new Integer(10));
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());
  
  link.addFirst(new Integer(20));
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());

  link.addLast("c");
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());

  link.add(2,"j");
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());

  link.add(1,"t");
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());

  link.remove(3);
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());
  }
}

Output of the program will be like this:

The contents of array is[a, b, 10]
The size of an linkedlist is3
The contents of array is[20, a, b, 10]
The size of an linkedlist is4
The contents of array is[20, a, b, 10, c]
The size of an linkedlist is5
The contents of array is[20, a, j, b, 10, c]
The size of an linkedlist is6
The contents of array is[20, t, a, j, b, 10, c]
The size of an linkedlist is7
The contents of array is[20, t, a, b, 10, c]
The size of an linkedlist is6

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 7, 2007

Related Tutorials

Discuss: Link List Example in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
Emily
July 3, 2011
ZZFXlgUCoLSJlTv

I can't believe I've been going for years withuot knowing that.
ankita
August 28, 2011
npe

it will give null pointer exception when you add any elements.
maryam
December 29, 2011
javaquestion

How do I implement classes of arraylist and linkedlist in Java?
maryam
December 29, 2011
javaquestion

How do I implement classes of arraylist and linkedlist in Java?
KANAGANA NYABAKORA B.
May 8, 2012
Group Names for Assignment is 133 2011-2012

I would like to known which strategies I can be used to create a class and method by using the Link List in Java language so as to approach as a programmer. Thanks you very much good morning.
harsh20
July 30, 2012
complain

ur codes doesnt run in netbeans.change ur code style.some of them did work bt this is not running in netbeans
DMCA.com