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

Generic Swap Method

mediumGenerics, Methods

📄 Code

Read carefully — what does this print?
1import java.util.Arrays;
2 
3public class Main {
4 public static <T> void swap(T[] arr, int i, int j) {
5 T temp = arr[i];
6 arr[i] = arr[j];
7 arr[j] = temp;
8 }
9 
10 public static void main(String[] args) {
11 String[] words = {"cat", "dog", "bird"};
12 swap(words, 0, 2);
13 System.out.println(Arrays.toString(words));
14 }
15}

🎯 Your Answer