~/ learn/ java-from-zero/ cards/ Wildcards and PECS: producer-extends, consumer-super
1 of 4

Type a method that reads from a wildcard-bounded list

Type a method that reads from a wildcard-bounded list

Answer

public static double sum(List<? extends Number> list) { double total = 0; for (Number n : list) total += n.doubleValue(); return total; }

? extends Number — the list is a producer of Numbers. You read elements as Number but cannot add to it.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/4be45e35-c110-4e2f-9205-5c7609b77ef3/flashcard utf-8 LF