I tried to write a sorting generic method, but it turned out that I needed to use compareTo. But when the compiler was compiling, it was found that it could not be used.
Later, when I was flipping through the book, I discovered that
For generic construction arrays, new
str=(T[])Array.newInstance(a.getClass().getComponentType(),a.length);进行构造
. For initialization of generic classes, use
sorts<String>sort1=new sorts<>(str);
for generics. type, if you want to use compareTo, you cannot use the int class, you have to use the Integer class
Integer[] number=new Integer[lo];
Take quick sort as an example, the source code is as follows!
import java.lang.reflect.Array; import java.util.Arrays; import java.util.Scanner; class sorts{ T[] str; sorts(T[]a){ str=(T[])Array.newInstance(a.getClass().getComponentType(),a.length); for(int i=0;i =0)lo++; if(lo sort2=new sorts<>(number); sort2.quick_sort(0,number.length); // Arrays.sort(number); for(int i=0;i Copy after login
Related articles:
About generic classes, generic methods, and generic interfaces in java
The above is the detailed content of Detailed explanation of cases using generic methods in java. For more information, please follow other related articles on the PHP Chinese website!