Using the ? wildcard : Generic Parameters « Generics « Java Tutorial






import java.util.ArrayList;
import java.util.List;

public class MainClass {
    public static void printList (List<?> list) {
        for (Object element : list) {
            System.out.println(element);
        }
    }
    public static void main(String[] args) {
        List<String> list1 = new ArrayList<String>();

        list1.add ("Hello");
        list1.add ("World");
        printList (list1);

        List<Integer> list2 = new ArrayList<Integer>();
        list2.add(100);
        list2.add(200);
        printList(list2);
    }
}
Hello
World
100
200








12.4.Generic Parameters
12.4.1.Using the ? wildcard
12.4.2.Using Bounded Wildcards in Methods
12.4.3.Multiple Type Parameters
12.4.4.Type Parameter Bounds
12.4.5.Constraints on a Wildcard