Aishu Aishu - 5 months ago 12
Java Question

Compilation error: Type List can't be Generic

I'm new to Java programming, I tried a sample program, the source is found from here:

List<String> list = new ArrayList<String>(); // Compile time error

String string1 = "a string";
list.add(string1);

String string2 = list.get(0);


Getting compile time error

Error Description:- The type List is not generic; it cannot be parameterized with arguments <String>


I'm not sure about this error. Can anyone explain?

Answer

You are importing the wrong List. Try the following*:

java.util.List<String> list = new java.util.ArrayList<String>();

If it works, just replace the List import with the java.util one.

Note that the java.awt.List is not generic, and that's why you're getting the error.

* If you're using Java 7+, you can use the diamond:

List<String> list = new ArrayList<>();