public class Demo1 {
public static void main(String[] args) {
int[] arr1 = new int[]{0,1,2,3,4,5};
swap(arr1,2,3);//这里报错了,为什么?
System.out.println(arr1);
}
public static <T> void swap(T arr[],int num1, int num2) {
T temp = arr[num1];
arr[num1] = arr[num2];
arr[num2] = temp;
}
}
------解决思路----------------------
public static void main(String[] args) {
Integer[] arr1 = new Integer[] { 0, 1, 2, 3, 4, 5 };
swap(arr1, 2, 3);
System.out.println(Arrays.toString(arr1));
}
public static <T> void swap(T[] arr, int num1, int num2) {
T temp = arr[num1];
arr[num1] = arr[num2];
arr[num2] = temp;
}
这里需要用包装类