~/ 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/19109465-fc7b-4a14-b58f-6024147b79f5/flashcard utf-8 LF