import java.util.Arrays;
import java.util.Comparator;public class StaticInnerClassDemo {
public static void main(String[] args) {
String[] names = {"郭","郭美","郭美美"};
Arrays.sort(names, new ByLength());
System.out.println(Arrays.toString(names));
}
    static class ByLength implements Comparator<String> {
    public int compare(String o1, String o2) {
   return o2.length() - o1.length();
    }
    }上面的代码Arrays.sort(names, new ByLength())为什么会调用compare函数,而函数又是怎么对names数组进行排序的呢?

解决方案 »

  1.   

    java源代码公开,直接点进去看不就行了。
      

  2.   

    我查过java API,Arrays.sort()没有这种Arrays.sort(names, new ByLength())用法啊。
      

  3.   

    调用的   public static <T> void sort(T[] a, Comparator<? super T> c)
      

  4.   

     Comparator<? super T> c是什么意思。
      

  5.   

    泛型啊,<? super T>表示Comparator里装的是T的父类
      

  6.   

    找不到方法就进入到他的父类找。或者直接Ctrl+鼠标点击就进去看了。
      

  7.   

    泛型啊,<? super T>表示Comparator里装的是T的父类那后面的C呢?
      

  8.   

    泛型啊,<? super T>表示Comparator里装的是T的父类那后面的C呢?
    不就是个参数名吗。。
      

  9.   

    泛型啊,<? super T>表示Comparator里装的是T的父类那后面的C呢?
    不就是个参数名吗。。哦,哈哈,谢谢你。
      

  10.   

    泛型啊,<? super T>表示Comparator里装的是T的父类还有一个问题, new ByLength()跟Comparator<? super T> c有什么关系呢?
      

  11.   

    ByLength implements Comparator
      

  12.   

    我的意思是说,在public static <T> void sort(T[] a, Comparator<? super T> c)中,第二个参数是Comparator<? super T>(表示Comparator中装的是T的父类),但是在主函数调用的时候是Arrays.sort(names, new ByLength());第二个参数是new ByLength(),它与Comparator<? super T> c是同类型的吗?
      

  13.   

    说白了,我想问的就是new ByLength()并不是String的父类或者String本身啊。
      

  14.   

    没看懂
    /**
         * Sorts the specified array of objects according to the order induced by
         * the specified comparator.  All elements in the array must be
         * <i>mutually comparable</i> by the specified comparator (that is,
         * <tt>c.compare(e1, e2)</tt> must not throw a <tt>ClassCastException</tt>
         * for any elements <tt>e1</tt> and <tt>e2</tt> in the array).<p>
         *
         * This sort is guaranteed to be <i>stable</i>:  equal elements will
         * not be reordered as a result of the sort.<p>
         *
         * The sorting algorithm is a modified mergesort (in which the merge is
         * omitted if the highest element in the low sublist is less than the
         * lowest element in the high sublist).  This algorithm offers guaranteed
         * n*log(n) performance.
         *
         * @param a the array to be sorted
         * @param c the comparator to determine the order of the array.  A
         *        <tt>null</tt> value indicates that the elements'
         *        {@linkplain Comparable natural ordering} should be used.
         * @throws  ClassCastException if the array contains elements that are
         * not <i>mutually comparable</i> using the specified comparator.
         */
        public static <T> void sort(T[] a, Comparator<? super T> c) {
    T[] aux = (T[])a.clone();
            if (c==null)
                mergeSort(aux, a, 0, a.length, 0);
            else
                mergeSort(aux, a, 0, a.length, 0, c);
        }
      

  15.   

    Comparator<String> bl = new ByLength();
    Arrays.sort(names, bl);
    从 public static <T> void sort(T[] a, Comparator<? super T> c), 你可以蒙出来new ByLength()是Comparator<? super T>
      

  16.   


    sort(String[] a, Comparator<String> c) sort要的参数类型是 Comparator<String>.Comparator<T> 里的T是String(这不叫参数).
    public interface Comparator<T> {
        
        int compare(T o1, T o2);   
        boolean equals(Object obj);
    }
      

  17.   

    debug调试下,然后查看相关api吧。
      

  18.   

    Comparator是个比较器,<String>说明比较的对象是String类型的
      

  19.   

    因为你调用的是Arrays.sort(names, new ByLength());内的new ByLength(),实例化一个对象,这个对象重写了compare()方法
      

  20.   

    不同数据类型的比较大小方法不同,百度下快速排序C库实现,它的最后一个参数也是compare函数。
      

  21.   

    new ByLength()相当于创建了ByLength这个内部类的实例,ByLength类又实现了Comparator类,重写了Comparator类中compare方法,使其返回一个数值,查遍API,发现貌似只有这个与之匹配,
    public static <T> void sort(T[] a, Comparator<? super T> c)