LinkedList in Java
By: Dorris Printer Friendly Format
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());
}
}
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program 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