反射代码。
来源:1-5 选择排序法的复杂度分析
无心铁憨憨
2020-07-14 23:08:21
public static <E extends Comparable> void sortTest(Class clazz,String sortName,E[] arr){
try {
SortInterface sortInterface = (SortInterface) clazz.getConstructor().newInstance();
long startTime = System.nanoTime();
sortInterface.sort(arr);
long endTime = System.nanoTime();
double time = (endTime - startTime) / 1000000000.0;
System.out.println(String.format("%s: n = %d, 执行时间 = %f s", sortName,arr.length,time));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("class newInstance failed");
}
}1回答
感谢分享:)
继续加油!:)
相似问题