I am learning about generics so I made a simple java program so I can learn how they exactly work. But now I am stuck with this program because I can't see why it won't work.
public static void main(String[] args)
{
ArrayList<String> strings = new ArrayList<>();
strings.add("A");
strings.add("B");
printList(strings);
}
public static <E extends List<E>> void printList(ArrayList<E> l)
{
for (E obj : l)
{
System.out.println(obj.toString());
}
}
Erequired to extendList<E>?Eto extendList<E>. Which is not true forString.ArrayList<? extends List<E>>. Either replace the parameter type withE, or drop the bound onE.ArrayList<List<E>>. A list of lists, basically. Then you pass it a list ofString. Just take out the<E extends List<E>>part and you should be good to go.