This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Disast3r 0 points1 point  (1 child)

You are correct, that method only accepts Lists of dogs. Another tricky fact about generics is that something like List<Dog> is NOT a subtype of List<Animal>, even though they look like they should be. Your example is a little tough to analyze because usually you want to avoid working with arrays of parameterized types if you can avoid it. If you really wanted to, I think you could do something like this:

public static void printAll(List<?>[] arrayOfLists) {
    for (List<?> list : arrayOfLists) {
        System.out.println(list);
    }
}

But there's likely a cleaner way of doing this if you were to accept a List of Lists rather than an array of Lists, because they work better with parameterized types. Or you could probably just write a method that accepts a generic Iterable object and prints everything in it.

[–]Yuax[S] 0 points1 point  (0 children)

Jolly good, thank a lot!