Reputation: 3098
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.
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();
}
}
Upvotes: 13
Views: 43334
Reputation: 234795
You need to import java.util.List
instead of 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:
List<String> list = new ArrayList<String>();
Upvotes: 4
Reputation: 1866
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.
Upvotes: 1
Reputation: 372764
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.
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!
Upvotes: 5
Reputation: 340723
Because java.util.ArrayList
extends java.util.List
, not java.awt.List
. You are importing the wrong class:
import java.awt.List;
vs.
import java.util.List;
Upvotes: 2