java 有什么问题: LinkedList<String> stringList = new LinkedList<String>();

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1217868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:41:13  来源:igfitidea点击:

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

java

提问by Eric Wilson

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?

我错过了什么?你不能这样做吗?

回答by newacct

Check to make sure you don't have a compiled class named LinkedListin 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.LinkedListis not imported.

检查以确保您没有LinkedList在同一目录中命名的已编译类。(特别是因为“链表”是一个常用术语,人们经常在初学者时尝试实现它。)如果您使用类似的东西导入类,这一点很重要import java.util.*;,因为*按需导入,所以如果有已在包中具有相同名称的类,则使用该类并且java.util.LinkedList不导入该类。

回答by cletus

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)?

您是否可能针对 JDK 1.4 或更早版本进行编译?或者您是否将构建或 IDE 中的语言设置设置为 5.0 之前的版本(因此不支持泛型)?

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.

话虽如此,假设您正在针对 JDK 5.0+ 进行编译,将您的语言设置设置为 Java 5+,并且这是一个 java.util.LinkedList,那么您的代码是完全有效的。

回答by saheer

Don't take the class name as class LinkedListinstead you can take class LinkedListDemoand rest of the declaration LinkedList<String> t = new LinkedList<String>();should be there and it will compile perfectly.

不要取类名,因为class LinkedList您可以取而代之class LinkedListDemo,其余的声明LinkedList<String> t = new LinkedList<String>();应该在那里,它会完美编译。

回答by the hero100

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"

我遇到了同样的问题,我发现我错误地使用了: import java.awt.List; 并收到以下错误消息:“类型列表不接受参数”

Use this instead

改用这个

import java.util.List;

导入 java.util.List;

回答by TARUN NAGIA

You have used import java.util.*;

你用过 import java.util.*;

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

如果您使用,您将不会遇到任何问题 import java.util.LinkedList;