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));
		}
	}
}
 
Code:
import java.util.LinkedList;
import java.util.List;

public class Listy
{
    public static void main(String[] args)
    {
        List<String> list = new LinkedList<String>();

        list.add("Homer");
	list.add("Marge");
	list.add("Bart");
	list.add("Lisa");
	list.add("Maggie");
        
        for (String s : list)
        {
            System.out.println(s);
        }
        
    }
}

Why not just do it like this?

If you want a stack use Stack class and pop them off properly.

For future reference your for loop is retarded, your incrementing n but removing the items as well.. That is your bug.
 
Last edited:
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:
int is not allowable as int is not an object (it's a primitive). The generic parameter (the <String>) must be an object. If you wanted to put integers into a collection then you'd need to parametrise with the <Integer> wrapper class. What the generic does is stop you from having to cast when items are returned from the collection, it also forces the collection to only allow that type of object to be added.

Also, if you don't want to use the new for-each loop construct like Una did then it's:

Code:
for ( int i = 0; i < stack.size(); i++ ) {
System.out.println(stack.get(i));
}

And like he said, if you want LIFO access then use:
http://java.sun.com/javase/6/docs/api/java/util/Stack.html
 
Last edited:
The new for-loop (called an enhanced for-loop or for-each loop) Una mentioned was introduced in Java 5 and replaced the need to use an Iterator object as it's handled behind the scenes. Although Java 5 has been around for some time there are still a lot of old material around that uses the old style methods so I can see why someone learning can be confused.

The angle brackets such as <String> are refered to as generics and are another feature of Java 5 and beyond and also suffers from having a lot of outdated material that doesn't mention it. Generics are esentially used to maintain type safety when compiling code and can prevent some nasty bugs creeping in that might otherwise be hard to detect. For example, if you initialize a LinkedList as containing Strings LinkedList<String> then operations such as get() can guarantee the type of the return value as a String.
 
Last edited:
Back
Top Bottom