← Back to Question List
Java Code Walkthrough #44
Generic printAll Method
📄 Code
Read carefully — what does this print?1import java.util.ArrayList;2import java.util.List;34public class Main {5 public static <T> void printAll(List<T> list) {6 for (T item : list) {7 System.out.println(item);8 }9 }1011 public static void main(String[] args) {12 List<String> words = new ArrayList<>();13 words.add("cat");14 words.add("dog");15 printAll(words);1617 List<Integer> nums = new ArrayList<>();18 nums.add(10);19 nums.add(20);20 printAll(nums);21 }22}