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. android - NavigationView 的側滑菜單中如何保存新增項(通過程序添加)2. javascript - 微信公眾號網頁使用redux如何管理用戶刷新?3. 提示語法錯誤語法錯誤: unexpected ’abstract’ (T_ABSTRACT)4. tp5 不同控制器中的變量調用問題5. 這段代碼既不提示錯誤也看不到結果,請老師明示錯在哪里,謝謝!6. php - 第三方支付平臺在很短時間內多次異步通知,訂單多次確認收款7. php7.3.4中怎么開啟pdo驅動8. mysql服務無法啟動1067錯誤,誰知道正確的解決方法?10. 老師 我是一個沒有學過php語言的準畢業生 我希望您能幫我一下
