Can't assign an ArrayList object to List reference type?

2058 views java
4

Here are my imports:

import java.awt.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

I tried this and there is some error saying:

The type List is not generic; it cannot be parameterized with arguments

List<Integer> myList = new ArrayList<Integer>();

answered question

You are likely compiling with a lower version of Java. Check your IDE settings.

Check the imports!

Either that or you're using the wrong List class. Yes, check the imports as per @Pavitra

Yes, you're importing the wrong list type

3 Answers

4

Generics started in 2004 within version J2SE 5.0. Check your List and ArrayList imports and the Java compiler version. To get the code compiles you should use a Java >5 version

posted this
9

Check it out:

import java.util.ArrayList;
import java.util.List;

List<Integer> myList = new ArrayList<>();

posted this
3

can you please share the import which you are using for List interface. this would be the possible reason fro this error. you should use

import java.util.List;

for list interface

and for ArrayLIst you have to use below import

import java.util.ArrayList;

posted this

Have an answer?

JD

Please login first before posting an answer.