LinkedList (Java 1.4)

import java.util.*; 

public class LinkedListTest {
    public static void main( String [] args) {
	LinkedList mylist = new LinkedList();

	// adding some Integers
	mylist.add(new Integer(5));
	mylist.add(new Integer(3));
	mylist.add(new Integer(1));

	Integer myInt = (Integer) mylist.getFirst();
	System.out.println(myInt);

	myInt = (Integer) mylist.getLast();
	System.out.println(myInt);

	mylist.removeLast();

	// iterating through LinkedList

	ListIterator iterator = mylist.listIterator(0);

	while(iterator.hasNext()) {
	    myInt = (Integer) iterator.next();
	    System.out.println(myInt);
	}
    }
}
Java 5:

import java.util.*; 

public class LinkedListTest2 {
    public static void main( String [] args) {
	LinkedList<Integer> mylist = new LinkedList();

	// adding some Integers
	mylist.add(new Integer(5));
	mylist.add(new Integer(3));
	mylist.add(new Integer(1));

	Integer myInt = mylist.getFirst();
	System.out.println(myInt);

	myInt = mylist.getLast();
	System.out.println(myInt);

	mylist.removeLast();

	// iterating through LinkedList

	for (Integer myInt2 : mylist) {
	    System.out.println(myInt2);
	}
    }
}

This is an example from CSci 3601 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.