Cannot resolve constructor 'ArrayList(java.util.List<T>)` in Java

Jaquarh :

I am trying to create an array list of Libraries my application uses to create an instance of. I have an abstract class which they extend so I can use the same Class<T>

abstract class Library {}
abstract class SomeLib extends Library {}

I now want to create this array list, this is how I am trying to do it:

public ArrayList<Class<Library>> libs = new ArrayList<Class<Library>>(
        Arrays.asList(SomeLib.class)
);

However, I get this error:

Cannot resolve constructor 'ArrayList(java.util.List)`

What am I doing wrong?

Johannes Kuhn :

As I already said in the comments, use ArrayList<Class<? extends Library>>.

The reason for this is simple: SomeLib.class has the type Class<SomeLib>, which is not a Class<Library>.

But if you make the generic a bit looser, by allowing it's subclasses (as you want to do), then Class<SomeLib> matches Class<? extends Library>, and everything works fine.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=311217&siteId=1