tags:

views:

584

answers:

3

When I try:

LinkedList<String> stringList = new LinkedList<String>();

I get the following compilation error:

type LinkedList does not take parameters

What am I missing? Can't you do this?

+8  A: 

Are you possibly compiling against a JDK 1.4 or earlier? Or do you have your language setting in your build or IDE set to pre-5.0 (so no generics support)?

By the way, the best way to do that is

List<String> stringList = new LinkedList<String>();

Use the interface rather than the implementation wherever possible.

That being said, assuming you're compiling against a JDK 5.0+, have your language settings set to Java 5+ and that is a java.util.LinkedList then your code is perfectly valid.

cletus
Actually, the best way to do that is to use Google Collections :-)`List<String> stringList = Lists.newLinkedList();`
ChssPly76
Hmm . . . I'm using JDK 7. I'll try compiling with JDK 6. I'd hate to say that I've found a bug in the compiler, but it isn't yet officially released, so it's possible.
FarmBoy
Classic newbie mistake: "I've found a bug in the compiler." It might be possible, but it's vanishingly small compared to probability that you're the one who's made the mistake. Banish the thought - it'll be the best thing you learn from this episode
duffymo
Open a command shell, type "java -version", and see what you get. That's the only way to be sure about the version you're using.
duffymo
Also work out what your JAVA_HOME is. echo %JAVA_HOME% on Windows, typically echo $JAVA_HOME on Linux/Unix (at a command line).
cletus
I'm a bit embarrassed to have publicly blamed the compiler. In any case, Mr. newacct guessed my problem.
FarmBoy
+6  A: 

Check to make sure you don't have a compiled class named LinkedList in the same directory. (Especially since "linked list" is a common term, and it is something that people often try to implement as beginners.) This is important if you import your classes using something like import java.util.*;, because the * imports on-demand, so if there is a class with the same name in the package already, then that class is used and the java.util.LinkedList is not imported.

newacct
Or better yet, look at your imports and make sure you're actually importing java.util.LinkedList and not some other LinkedList
MatrixFrog
You guessed it, good job.Just the other day, I had coded a simple LinkedList for practice, I shouldn't have forgotten so soon.
FarmBoy
A: 

Don't take the class name as " class LinkedList" instead you can take "class LinkedListDemo" and rest of the declaration "LinkedList(String> t= new LinkedList(String>(); " should be there and it will compile perfectly. Note: Please don't use '(' braket for (String> instead use only <>. I don't know there is some technical fault in writing and it is not acceppting'<' this bracket.

saheer
When you type an answer, read the notes about using markdown. Then you will be able to type `LinkedList<String>` and view it correctly.In any case, thanks for your contribution.
FarmBoy