1. Home
  2. Computing & Technology
  3. Focus on Java

Java Programming Tutorial: Creating a Linked List

From Earline Gilley, for About.com

1 of 10

Learn About LinkedList

Java has a framework, or a set of classes, that help the programmer contend with collections. Collections are, as you might imagine, groups of similar objects (or primitive data types). In this tutorial, we’ll create a LinkedList and then traverse the list using both an Iterator and a ListIterator.

What’s the Difference Between an Iterator and a ListIterator?

ListIterator has a few additional methods that make scrolling through your LinkedList a little easier. Those additional methods are:

  • add()- method that lets you insert while traversing the list
  • nextIndex()- returns an int representing the index of the following element
  • hasPrevious()- returns a Boolean value (true or false) indicating whether there is an element in the reverse direction
  • previous()- returns the element before the current element
  • previousIndex() - if there is an element before the current one, returns the index of that element

First, you will need to create a new file in your text editor. Next, import the classes you will need to use for this program by typing the following in your new text file:

import java.util.LinkedList;
import java.util.Iterator;
import java.util.ListIterator;

1 of 10

Explore Focus on Java

More from About.com

  1. Home
  2. Computing & Technology
  3. Focus on Java
  4. Tutorials
  5. Java Programming Tutorial: Creating a Linked List

©2008 About.com, a part of The New York Times Company.

All rights reserved.