Aishu
Aishu

Reputation: 1320

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?

Upvotes: 2

Views: 90

Answers (5)

Maroun
Maroun

Reputation: 95968

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

Upvotes: 7

ThiepLV
ThiepLV

Reputation: 1279

You can try:

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

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

String string2 = list.get(0);

Because it must be:

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

NOT

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

Upvotes: -1

heyDude
heyDude

Reputation: 178

Looks like you forgot round brackets

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

even if it's generic - you must use usual constructor with\without parameters.

Upvotes: -1

vv88
vv88

Reputation: 173

List definition is wrong. It should be like below -

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

Putting () let you create the object.

Upvotes: 0

Azodious
Azodious

Reputation: 13872

Change

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

to

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

Upvotes: 1

Related Questions