Which fills in the blank in the method signature to allow this code to compile?
import java.util.*; public class Main { private static <___, U> U add(T list, U element) { list.add(element);//w ww. j av a2s . c om return element; } public static void main(String[] args) { List<String> values = new ArrayList<>(); add(values, "A"); add(values, "A"); add(values, "B"); System.out.println(values); } }
C.
The ? is an unbounded wildcard.
It is used in variable references but is not allowed in declarations.
In a static method, the type parameter specified inside the <> is used in the rest of the variable declaration.
Since it needs an actual name, Options A and B are incorrect.
We need to specify a type constraint so we can call the add()
method.
Regardless of whether the type is a class or interface, Java uses the extends keyword for generics.
Option D is incorrect, and Option C is the answer.