What's wrong with: LinkedList<String> stringList = new LinkedList<String>(); - java

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?

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.

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.

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.

I had a same problem and I figured out that I mistakenly used:
import java.awt.List;
and got the following error message:
"Type List doesn't take paramaters"
Use this instead
import java.util.List;

You have used import java.util.*;
You will not face any issue if you use import java.util.LinkedList;

Type something like this :
import java.util.LinkedList;
class LinkedListDemo {
public static void main(String[] args) {
LinkedList <String> list=new LinkedList <String> ();
}}

Related

Facing a problem with using Natty library for converting string to date [duplicate]

I get the following error despite it's exactly one used in examples:
error: type List does not take parameters
List<String> strings_wat = new ArrayList<String>();
Java is version 1.7 and the class is NOT named ArrayList.
You are likely importing java.awt.List.
You should instead import java.util.List, which is a parameterized type.
It seems like you are importing it from java.awt:
import java.awt.List;
and it looks like you want to use the one from java.util:
import java.util.List;
if you are working on Graphical user interface you have to
import java.awt.List instead of import java.util.List
Other then else if you are working on simple code you have to import java.util.List; instead of import java.awt.List
I am getting solved this problem by renaming the class name or file name as in Notepad++.
You Should not take the class names as ArrayList or List.
And you also need to add the following package
import java.util.*;
My Previous class name is List I changed it into the Names.so You have to change the class name.

Getting recursively all components on JFrame [duplicate]

I get the following error despite it's exactly one used in examples:
error: type List does not take parameters
List<String> strings_wat = new ArrayList<String>();
Java is version 1.7 and the class is NOT named ArrayList.
You are likely importing java.awt.List.
You should instead import java.util.List, which is a parameterized type.
It seems like you are importing it from java.awt:
import java.awt.List;
and it looks like you want to use the one from java.util:
import java.util.List;
if you are working on Graphical user interface you have to
import java.awt.List instead of import java.util.List
Other then else if you are working on simple code you have to import java.util.List; instead of import java.awt.List
I am getting solved this problem by renaming the class name or file name as in Notepad++.
You Should not take the class names as ArrayList or List.
And you also need to add the following package
import java.util.*;
My Previous class name is List I changed it into the Names.so You have to change the class name.

LinkedList in Java and imports

I'm trying to make a linked list in Java, but there is an issue.
I thought that Link is in java.util.Link,but when I tried to do something like
Link nextLink;
Eclipse wanted to
import sun.awt.image.ImageWatched.Link;
I think that this import is different from what I expect and if I import java.util.Link it gives error.
use
import java.util.LinkedList;
Do you need something like this?
import java.util.LinkedList;
public class LinkTest {
private LinkedList<Object> list = new LinkedList<Object>();
}

error: type List does not take parameters

I get the following error despite it's exactly one used in examples:
error: type List does not take parameters
List<String> strings_wat = new ArrayList<String>();
Java is version 1.7 and the class is NOT named ArrayList.
You are likely importing java.awt.List.
You should instead import java.util.List, which is a parameterized type.
It seems like you are importing it from java.awt:
import java.awt.List;
and it looks like you want to use the one from java.util:
import java.util.List;
if you are working on Graphical user interface you have to
import java.awt.List instead of import java.util.List
Other then else if you are working on simple code you have to import java.util.List; instead of import java.awt.List
I am getting solved this problem by renaming the class name or file name as in Notepad++.
You Should not take the class names as ArrayList or List.
And you also need to add the following package
import java.util.*;
My Previous class name is List I changed it into the Names.so You have to change the class name.

What is an 'ambiguous type' error in Java?

In the following code, I get an error from the compiler on the last line that says: 'the type List is Ambiguous' (on the line that attempts to define cgxHist list). What am I doing wrong?
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class drawr extends JPanel{
public static int animationSpeed=470;
public static int diameter = 50;
hBod allHBods[];
List<String> cgxHist = new ArrayList<String>();
I actually wanted the list to contain integers, but when I try to 'cast' the list as such, by replacing <String> with <int>, the error on that line becomes 'Syntax error on token "int", Dimensions expected after this token'. Advice please.
The problem is that there is a List class in both the java.awt and the java.util package, and as you are importing all classes in those packages, the compiler doesn't know which one you mean.
So you should either not use the asterisk to import all classes at the same time (just import the ones you actually need) or instead of List write java.util.List<String> cgxHist = new ArrayList<String>();
java.awt.List
java.util.List
Both of these exist. You'll have to add the namespace in front to use one:
java.util.List<String> cgxHist = new ArrayList<String>();
If you don't, it doesn't know how to interpret the List<T>: is it the awt one or util? Ergo: ambiguous.

Categories

Resources