package chapter1; import java.util.Comparator; public class FindMax2 { public static void main (String [] args ){ String [] arr ={"z","q","a"}; System.out.println(FindMax(arr, new CaseInsentiveCompare())); } public static <T> T FindMax (T[] arr, Comparator<? super T> cmp){ int maxIndex =0; for (int i =0; i<arr.length;i++){ if (cmp.compare(arr[i], arr[maxIndex])>0){ maxIndex = i; } } return arr[maxIndex] ; } } class CaseInsentiveCompare implements Comparator<String>{ public int compare(String lhs, String rhs) { // TODO Auto-generated method stub return lhs.compareToIgnoreCase(rhs); } }
为什么java 不支持泛型数组
参考下面的
http://www.blogjava.net/sean/archive/2005/08/09/9630.html
http://www.blogjava.net/deepnighttwo/articles/298426.html