标签:
ArrayList list = new ArrayList();
list.add("abc");
Integer num = (Integer) list.get(0); //运行时会出错,但编码时发现不了
list.add(new Random());
list.add(new ArrayList());
for(int i=0;i<list.size();i++){
(?)list.get(i); //此处取出来的对象应转换成什么类型
}
整个ArrayList<Integer>称为参数化的类型ParameterizedTyp
public class GenericDemo1 {
//泛型类型使用前必须先声明
//<T>就是声明的泛型类型,放在返回值的前面
public <T> T m1(){
return null;
}
public <T> void m2(T t){
}
public <T> void m3(Class<T> clazz){
}
}
//类上声明泛型类型,实例方法就可以直接使用了,方法上面不用再声明
public class GenericDemo2<T> {
public T m1(){
return null;
}
public void m2(T t){
}
public void m3(Class<T> clazz){
}
//静态方法都必须先声明才能使用
public static <T> void m4(T t){
}
//声明多个泛型,一般用在Map结构上
public static <K,V> V getKey(K k){
return null;
}
}
泛型使用小例子
public class ArrayUtil {
//定义一个方法,实现任意数组指定位置上的元素交换
// 1 2 3 4 5 1 5 3 4 2 1和4元素交换
// int[] byte[] short[] long[]... Object[] 不用写9个方法
public static <T> void exchange(T[] t,int index1,int index2){
T temp = t[index1];
t[index1] = t[index2];
t[index2] = temp;
}
//反转数组中的元素: 1 2 3 4 ----》4 3 2 1
public static <T> void reverse(T[] t){
int startIndex = 0;
int endIndex = t.length-1;
while(startIndex<endIndex){
exchange(t, startIndex, endIndex);
startIndex++;
endIndex--;
}
}
}
//泛型测试
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Test;
public class ArrayUtilTest {
@Test
public void testExchange() {
//int ii[] ={1,2,3,4,5}; 错误的写法,使用泛型后不能使用基本类型
Integer ii[] = {1,2,3,4,5};
ArrayUtil.exchange(ii, 1, 4);
System.out.println(Arrays.asList(ii));
//结果:1 5 3 4 2 第二个位置和第四位置的数字交换了
}
@Test
public void testReverse() {
Integer ii[] = {1,2,3,4,5,6};
ArrayUtil.reverse(ii);
System.out.println(Arrays.asList(ii));
//测试中,奇数长度与偶数长度数组的结果没有问题
}
}
标签:
原文地址:http://www.cnblogs.com/move22/p/5734526.html