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?
You need the following 2 imports
Do you use an IDE? Most IDE's have helps that will suggest fixes like these imports.
You can press Shift+Ctrl+O for auto importing.
Put the following at the top of your source file:
Here is an explanation of what packages are and how the
import
statement works.You will have to
either import
the packages in which these classes are present,or write the entire path.
1. import :
2. Full path:
public class A {
java.util.List<Integer>
intList = newjava.util.ArrayList<Integer>()
;}
3. In Eclipse IDE,
use
ctrl + shift + O
to import.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 tounbound
.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.
You are declaring List object outside any method. Move it inside a method and it will work.