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. python bottle跑起來以后,定時執行的任務為什么每次都重復(多)執行一次?2. javascript - ios返回不執行js怎么解決?3. mysql - 分庫分表、分區、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處4. javascript - angular使從elastichearch中取出的文本高亮顯示,如圖所示5. javascript - 求幫助 , ATOM不顯示界面!!!!6. 前端 - 誰來解釋下這兩個 CSS selector 區別7. html5 - HTML代碼中的文字亂碼是怎么回事?8. node.js - vue中 post數據遇到問題9. javascript - vue2如何獲取v-model變量名10. python - 爬蟲模擬登錄后,爬取csdn后臺文章列表遇到的問題
