CJCoding With Joseph
15per day
← Back to Question List
Java Code Walkthrough #44

Generic printAll Method

mediumGenerics, Methods

📄 Code

Read carefully — what does this print?
1import java.util.ArrayList;
2import java.util.List;
3 
4public class Main {
5 public static <T> void printAll(List<T> list) {
6 for (T item : list) {
7 System.out.println(item);
8 }
9 }
10 
11 public static void main(String[] args) {
12 List<String> words = new ArrayList<>();
13 words.add("cat");
14 words.add("dog");
15 printAll(words);
16 
17 List<Integer> nums = new ArrayList<>();
18 nums.add(10);
19 nums.add(20);
20 printAll(nums);
21 }
22}

🎯 Your Answer