Eclipse message saying List cannot be resolved to

2020-02-25 06:49发布

I am trying the following code in eclipse:

public class A {    
    List<Integer> intList = new ArrayList<Integer>();
}

However it gives me an error saying: List cannot be resolved to a type and ArrayList cannot be resolved to a type.

Is there some library I need to add and how do I do that?

标签: java eclipse
6条回答
我只想做你的唯一
2楼-- · 2020-02-25 07:30

You need the following 2 imports

import java.util.ArrayList;
import java.util.List;

Do you use an IDE? Most IDE's have helps that will suggest fixes like these imports.

查看更多
beautiful°
3楼-- · 2020-02-25 07:32

You can press Shift+Ctrl+O for auto importing.

查看更多
淡お忘
4楼-- · 2020-02-25 07:37

Put the following at the top of your source file:

import java.util.ArrayList;
import java.util.List;

Here is an explanation of what packages are and how the import statement works.

查看更多
狗以群分
5楼-- · 2020-02-25 07:51

You will have to either import the packages in which these classes are present, or write the entire path.

1. import :

import java.util.ArrayList;
import java.util.List;

2. Full path:

public class A {

java.util.List<Integer> intList = new java.util.ArrayList<Integer>();

}

3. In Eclipse IDE,

use ctrl + shift + O to import.

查看更多
▲ chillily
6楼-- · 2020-02-25 07:51

Sometimes it may also be that the Java Build Path is not correctly set for your project.

Go to the project properties (Right click on project name in workspace and then click on Properties) and in the "Java Build Path" check if the JRE System Library is set to unbound.

If that's the case, change it to one of the Java SDK available on your machine. The address to download those is this one.

查看更多
Fickle 薄情
7楼-- · 2020-02-25 07:51

You are declaring List object outside any method. Move it inside a method and it will work.

查看更多
登录 后发表回答