类型不匹配:无法从ArrayList转换到List

时间:2023-01-12 13:15:39

I have only this, but my compiler says:Type mismatch: cannot convert from ArrayList to List So what is the problem can anyone tell me ? I'm using Elipse Java EE IDE.

我只有这个,但是我的编译器说:类型不匹配:不能从ArrayList转换到List那么有谁能告诉我什么问题?我正在使用Elipse Java EE IDE。

import java.awt.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;


public class Main {
    public static void main(String[] args) {
        List list = new ArrayList();


    }
}

5 个解决方案

#1


21  

incorrect import, it has to be java.util.List.

不正确的导入,必须是java.util.List。

#2


5  

You've imported java.awt.List, which is the list control in the AWT package, instead of java.util.List, which is the collections class representing a list of elements. Thus Java thinks you're converting from a logical array list of values into a widget, which doesn't make any sense.

你进口java.awt。List,它是AWT包中的List控件,而不是java.util。List,它是表示元素列表的集合类。因此,Java认为您正在将逻辑数组列表中的值转换为小部件,这没有任何意义。

Changing the import line to

将导入行更改为

import java.util.List;

should fix this, as would writing

应该像写作一样解决这个问题吗

java.util.List list = new ArrayList();

to explicitly indicate that you want a collection.

显式地指示您想要一个集合。

That said, you should also be using generics here. Using raw collections types has long been deprecated. The best answer is to write something like

也就是说,您应该在这里使用泛型。使用原始集合类型一直被弃用。最好的答案是写一些类似的东西。

List<T> list = new ArrayList<T>();

Hope this helps!

希望这可以帮助!

#3


3  

You need to import java.util.List instead of java.awt.List.

您需要导入java.util。而不是java.awt.List列表。

You might also want to use type parameters instead of raw types. For instance, if the list was going to hold String values:

您可能还希望使用类型参数而不是原始类型。例如,如果列表将保存字符串值:

List<String> list = new ArrayList<>();

or, prior to Java 7:

或者,在Java 7之前:

List<String> list = new ArrayList<String>();

#4


2  

Because java.util.ArrayList extends java.util.List, not java.awt.List. You are importing the wrong class:

因为java.util。ArrayList java.util延伸。列表,而不是java.awt.List。您导入了错误的类:

import java.awt.List;

vs.

vs。

import java.util.List;

#5


1  

As told by others its an import error. Since you are using Eclipse EDE if there is an error keep the cursor in that place and press Ctrl + 1, it will show suggestions for you which might help you in fixing the errors.

正如别人所说的,这是一个导入错误。由于您正在使用Eclipse EDE(如果出现错误,请将光标保持在该位置并按Ctrl + 1),它将为您显示可能有助于修复错误的建议。

#1


21  

incorrect import, it has to be java.util.List.

不正确的导入,必须是java.util.List。

#2


5  

You've imported java.awt.List, which is the list control in the AWT package, instead of java.util.List, which is the collections class representing a list of elements. Thus Java thinks you're converting from a logical array list of values into a widget, which doesn't make any sense.

你进口java.awt。List,它是AWT包中的List控件,而不是java.util。List,它是表示元素列表的集合类。因此,Java认为您正在将逻辑数组列表中的值转换为小部件,这没有任何意义。

Changing the import line to

将导入行更改为

import java.util.List;

should fix this, as would writing

应该像写作一样解决这个问题吗

java.util.List list = new ArrayList();

to explicitly indicate that you want a collection.

显式地指示您想要一个集合。

That said, you should also be using generics here. Using raw collections types has long been deprecated. The best answer is to write something like

也就是说,您应该在这里使用泛型。使用原始集合类型一直被弃用。最好的答案是写一些类似的东西。

List<T> list = new ArrayList<T>();

Hope this helps!

希望这可以帮助!

#3


3  

You need to import java.util.List instead of java.awt.List.

您需要导入java.util。而不是java.awt.List列表。

You might also want to use type parameters instead of raw types. For instance, if the list was going to hold String values:

您可能还希望使用类型参数而不是原始类型。例如,如果列表将保存字符串值:

List<String> list = new ArrayList<>();

or, prior to Java 7:

或者,在Java 7之前:

List<String> list = new ArrayList<String>();

#4


2  

Because java.util.ArrayList extends java.util.List, not java.awt.List. You are importing the wrong class:

因为java.util。ArrayList java.util延伸。列表,而不是java.awt.List。您导入了错误的类:

import java.awt.List;

vs.

vs。

import java.util.List;

#5


1  

As told by others its an import error. Since you are using Eclipse EDE if there is an error keep the cursor in that place and press Ctrl + 1, it will show suggestions for you which might help you in fixing the errors.

正如别人所说的,这是一个导入错误。由于您正在使用Eclipse EDE(如果出现错误,请将光标保持在该位置并按Ctrl + 1),它将为您显示可能有助于修复错误的建议。