CJCoding With Joseph
15free / daySign inCreate account
Back to Code Walkthrough

Generic Swap Method

Java code walkthrough · Question 43
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