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();
}
}
incorrect import, it has to be java.util.List
.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With