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 - iframe 為什么加載網(wǎng)頁的時候滾動條這樣顯示?2. 網(wǎng)頁爬蟲 - python+requests 網(wǎng)頁重定向求解3. 后端開發(fā) - mysql按時間分段統(tǒng)計的sql語句怎么寫好?4. 哭遼 求大佬解答 控制器的join方法怎么轉模型方法5. mysql - 在下剛入門sql 關于sql的語法詢問6. 請問寫好python模塊以后,文檔怎么寫?7. 老師您好!我有一個問題、8. c++ - 如何在python的阻塞的函數(shù)中獲取變量值9. list - python 求助10. 初來乍到,相對路徑問題,新手求教
