java泛型

泛型的使用

package com.tedu.day1302;

public class Study_T {
    public static void main(String[] args) {
        String[] arr1 = {"a","b","c","d"};
        Integer[] arr2 = {11,22,33,44};
        Double[] arr3 = {3.2,12.33,11.11};
        printArr(arr1);
        printArr(arr2);
        printArr(arr3);
    }
// 泛型,可以不确定传入的类型
    public static  <T>  void printArr(T[] t) {
        for (int i = 0; i < t.length; i++) {
            System.out.println(t[i]);

        }
    }
}

类中泛型的使用

package com.tedu.day1302;

import java.util.Arrays;

/**
 * 类使用泛型的例子
 */
public class Study_ClassType {
    public static void main(String[] args) {
        new Arr<Double>().toStr(new Double[]{1.11,2.22,32.44});
    }
}

class Arr<T>{
    public void toStr(T[] t){
        System.out.println(Arrays.toString(t));
    }
}
posted @ 2022-11-06 19:50  竹石2020  阅读(12)  评论(0编辑  收藏  举报