Why doesn't the following code work?
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
List<List<URL>> announces;
announces = new ArrayList<ArrayList<URL>>();
The error is the following:
Type mismatch: cannot convert from ArrayList<ArrayList<URL>> to <List<List<URL>>
Because your Generic is bounded to a type List<URL>
. i.e. only List
(which is an interface) is accepted.
You can allow any list by using wildcards.
List<? extends List<URL>> announces;
You can also consider subtyping. Example:
List<List<URL>> announces = new ArrayList<List<URL>>();
announces.add(new ArrayList<URL>());
announces.add(new LinkedList<URL>());
This is valid as the Generic type accepts a List<URL>
and ArrayList
, LinkedList
is-a List
.
Try this
List<? extends List<URL>> announces = new ArrayList<ArrayList<URL>>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With