Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type mismatch: cannot convert from ArrayList to List

Tags:

java

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();


    }
}
like image 516
Big.Child Avatar asked May 13 '12 21:05

Big.Child


2 Answers

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

like image 178
Betlista Avatar answered Oct 17 '22 19:10

Betlista


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!

like image 22
templatetypedef Avatar answered Oct 17 '22 20:10

templatetypedef