LinkedList in Java

By: Dorris Emailed: 1765 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

In this example, you will see the use of java.util.LinkedList class. We 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 six methods of LinkedList class is demonstrated.

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.

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());
	}
}

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Dorris
sum of all digits in a given number using Shell Script
sh -x and sh -v in Linux
case..esac..in Linux Shell Script
while loop in Linux Shell Script
Nested for loop in Linux Shell Script
for loop in Linux Shell Script
Multilevel if-then-else in Linux Shell Script
Nested if-else-fi in Linux Shell Script
if...else...fi in Linux Shell Script
test command or [ expr ] in Linux Shell Script
if condition in Linux Shell Script
Download Linux. Which linux and where?
Formula in Hibernate
Primary keys assigned by triggers in Hibernate
Assigned identifiers in Hibernate

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
A Serialization Example in Java
Type Casting in Java
instanceof sample program in Java
Using One-Dimensional Arrays in Java
Method Overriding in Java
indexOf( ) and lastIndexOf( ) in Java
The java Buzzwords
Write to a file in Java - Sample Program
XML and Java - Parsing XML using Java Tutorial
Student marks calculation program in Java
Handling Fractions in Java
Calculate average sale of the week in Java
Java program to check if user input is an even number
LinkedList in Java
Using the AWS SDK for Java
Most Emailed Articles (in Java)
Converting a number into its equalant value in words in Java
java.lang.reflect package
Disadvantages of using Native methods in Java
What is UCS? What is ISO 10646?
Generating Your Key Pair example using keytool in Java
Operator Precedence in Java
PushbackReader sample program in Java
concat(), replace(), and trim() Strings in Java
instanceof sample program in Java
Use of - new - in Java
The Benefits of OOP
Increment and Decrement Operator
Characters in java
The continuing Revolution of java
Why java is important to the Internet