1、编写一个泛型方法,实现指定位置上的数组元素的交换
2
、编写一个泛型方法,接受一个任意数组,并颠倒数组中的所有元素
package com.huanhuan.cn;
public class Demo2 {
//1、编写一个泛型方法,实现指定位置上的数组元素的交换
public void swap(T arr[],int pos1,int pos2){
T temp
= arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}
//2、编写一个泛型方法,接受一个任意数组,并颠倒数组中的所有元素
public void reverse(T arr[]){
int start = 0;
int end = 0;
while(start
T temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
}
**如果一个类的多处都要用到同一个泛型,这是可以把泛型定义在类上。
**静态方法不能使用类定义的泛型,而应单独定义泛型