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
相關(guān)文章:
1. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.2. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題3. docker-machine添加一個已有的docker主機問題4. docker - 如何修改運行中容器的配置5. java - Spring boot 讀取 放在 jar 包外的,log4j 配置文件,系統(tǒng)有創(chuàng)建日志文件,不寫入日志信息。6. angular.js - Angular 2 + Django構(gòu)建的Web應(yīng)用, 如何合理搭配 ?7. objective-c如何實現(xiàn)java中的反射機制8. css - 定位為absolute的父元素中的子元素 如何設(shè)置在父元素的下面?9. java - 請問在main方法中寫成對象名.屬性()并賦值,與直接參參數(shù)賦值輸錯誤是什么原因?10. android studio總是在processes running好久
