黑马程序员技术交流社区

标题: 有关泛型的练习题 [打印本页]

作者: sjw12025    时间: 2016-6-15 23:09
标题: 有关泛型的练习题
编写一个泛型方法,实现指定位置数组元素的交换
编写一个泛型方法,接收一个任意数组,并颠倒数组中的所有元素
注意:只有对象类型才能作为泛型方法的实际参数
在泛型中可以同时有多个类型
public <K,V> V getValue(K key) return { return map.get(key)}
案例:
package com.heima.generic;
//自定义泛型练习
public class ArraysUtil {
    //编写一个泛形方法,实现指定位置数组元素的交换
    public static <T> void exchange(T[] t,int index1,int index2){
        T temp = t[index1];
        t[index1] = t[index2];
        t[index2] = temp;
    }
    //编写一个泛形方法,接收一个任意数组,并颠倒数组中的所有元素
    public static <T> void reverse(T[] t){
        
        int startIndex = 0;
        int endIndex = t.length -1;
        
        while(startIndex<endIndex){
            T temp = t[startIndex];
            t[startIndex] = t[endIndex];
            t[endIndex] = temp;
            startIndex++;
            endIndex--;
        }
        
    }
}

        

作者: wrui    时间: 2016-6-22 22:17
顶一下,,,,,




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2