java - 一個泛型標簽問題
問題描述
新手問一個泛型問題
public static void main(String[] args) {ArrayList<Student> al = new ArrayList<>();al.add(new Student('大石榴',17,100));al.add(new Student('地雷',20,80));al.add(new Student('張大炮',21,60));Comparator<Student> cp = new Comparator<Student>() {@Override public int compare(Student o1, Student o2) {return o1.getAge() - o2.getAge(); }}; Collections.max(al, cp);//public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp)//這是max方法的源碼.// <T> 這個泛型在哪獲取到的?for(Student st : al){ System.out.println(st);} }
問題解答
回答1:Java中的泛型都是使用了類型擦除,你這里的<T> 只是一個類型變量。這個方法里面也只是用來代表@param <T> the class of the objects in the collection
相關文章:
1. javascript - 微信報redirect_uri參數錯誤2. 我在centos容器里安裝docker,也就是在容器里安裝容器,報錯了?3. android - 類似微信朋友圈或者QQ空間說說那種點擊圖片放大,并且有放大縮小手勢,左右滑動圖片手勢效果4. 網頁動畫等過渡效果,CSS3 transitions 和 jQuery animations 誰實現的性能更好?5. javascript - 怎么簡寫這段jQuery功能?6. angular.js - angular中的a標簽不起作用7. java - Atom中文問題8. java - 初學SSM 在import自己寫的包下的類的時候飄紅,求大神解答?9. javascript - 如何計算字符串寬度?10. win10系統 php安裝swoole擴展
