Java LinkedList

Soldato
Joined
27 Aug 2004
Posts
17,111
Location
Geordieland
Hi,

Im having a pla with the LinkedList facility, and am just looing to be able to do one thing. Is it possible to go through your LinkedList and output all of the values within the list?

Ive had a go with the following and its printing the first, third and final value, but not he ones inbetween?

Code:
import java.util.LinkedList;

class stack
{
	public static void main(String args[])
	{
		LinkedList stack = new LinkedList();
		
		stack.addFirst("Homer");
		stack.addFirst("Marge");
		stack.addFirst("Bart");
		stack.addFirst("Lisa");
		stack.addFirst("Maggie");
		
		System.out.println("In order as entered:");
		
		for(int n=0; stack.size()>n ; n++)
		{
			System.out.println("Stack Value:" + stack.size() + "\t\tValue: "+ stack.remove(n));
		}
	}
}
 
Yeah I know its wierd, its for uni, adn as shown I need to get cracking with the Java as my understanding of it is still pretty crap. Ive never seen it done the way you have done it though, so id guess we havent been told anything like that yet, but cheers, its makes more sense than the way I was trying to do it

I take it the <String> forces whatever is before it to be a String, so adding <int> would force the values to integer?

Cheers
 
Last edited:
Back
Top Bottom