0

This is my code:

private <T,L extends ArrayList<? extends T>> L Foo(L<T> list1) {
    L list2 = new ArrayList<T>();

    //more code

}

I'm using IDEA and get the following error message for the method parameter:

Type 'L' does not have type parameters

and the following error message for creating the ArrayList:

Incompatible types. <br>Required: L <br> Found: java.utils.ArrayList<T>;

Why does this happen? L extends ArrayList so it should have the same type parameters right?

Alex
  • 439
  • 5
  • 23
  • What is the purpose of `Foo`? Because `L` is defined to extend `ArrayList extends T>`, you can't assign any `ArrayList` to `L`. – rgettman Mar 01 '18 at 23:57
  • @rgettman This is part of a couple methods I am using to find unique objects in an input list of an arbitrary dimension. – Alex Mar 02 '18 at 00:00
  • you are trying to create `L`, which expects a class that extends an ArrayList, but youre trying to create `ArrayList`. – user2914191 Mar 02 '18 at 00:04
  • 2
    Your code doesn't make a lot of sense. It'll help if you explain what you're actually trying to do. – shmosel Mar 02 '18 at 00:19
  • 1
    `L` is a type variable. Type variables can't have type parameters of their own. (At least, not in Java.) If you want `L` to be a subtype of a parameterized type, you need to do that where you define `L` (e.g. `L extends List extends T>`). But you can't redefine the parameters of `L` elsewhere, and you can't require that `L` actually be a generic type -- it could be a concrete subclass of a generic class, for example. – Daniel Pryden Mar 02 '18 at 00:52

1 Answers1

-1

It's not known if ArrayList<T> is an L. For example, L could be a MyArrayListImpl<T>.

I think this is what you want:

private <T> List<T> Foo(List<T> list1) {
    List<T> list2 = new ArrayList<T>();

    //more code

}
Bohemian
  • 365,064
  • 84
  • 522
  • 658