Generic methods in Java
Forums:
static <T> void swap(T[] a, T[] b) {
T tmp = a[0];
a[0] = b[0];
b[0] = tmp;
}
Integer[] n1 = {5};
Integer[] n2 = {7};
swap(n1, n2);
//n1[0] is 7 and n2[0] is 5
String[] s1 = {"cat"};
String[] s2 = {"dog"};
swap(s1, s2);
//s1[0] is "dog" and s2[0] is "cat"
- Log in to post comments