简体   繁体   English

使用数组排序

[英]Sorting with Array

I am testing the different sort methods (Selection, bubble insertion) and I am trying to use Comparator at the same time. 我正在测试不同的排序方法(选择,气泡插入),我正在尝试同时使用Comparator。

So far I have two classes; 到目前为止,我有两节课; Main and Selectionsort. 主要和选择。

My main looks like this: 我的主要看起来像这样:

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String[] anArray = {"Ludo", "matador", "ChessTitan", "Rottefælden"};

        for (int i=0; i<anArray.length-1; i++) {
            for (int j=i+1; j<anArray.length; j++) {
                if (anArray[j].compareTo(anArray[i]) < 1) {

                    String temp = anArray[i];
                    anArray[i] = anArray[j];
                    anArray[j] = temp;

                }
            }

        }

        for (String string : anArray) {
            System.out.println(string);
        }
    }
}

And my selectionSort looks like this: 我的selectionSort看起来像这样:

public class SelectionSort implements Comparator<String> {

    @Override
    public int compare(String o1, String o2) {      
        return o1.compareTo(o2);
    }
}

What I want to do is use my Comparator when I use selection sort. 我想要做的是在使用选择排序时使用我的比较器。

How is this possible? 这怎么可能?

replace this line: 替换这一行:

if (anArray[j].compareTo(anArray[i]) < 1) {

with this: 有了这个:

if (comparator.compare(anArray[j],anArray[i]) < 1) {

where comparator is an instance of the comparator you want to use. 其中comparator是您要使用的比较comparator的实例。


Since your Comparator has no state, you'll probably want to assign it to a static final field. 由于您的比较器没有状态,您可能希望将其分配给静态最终字段。

private static final Comparator<String> COMP = new SelectionSort();

So now the above code would read 所以现在上面的代码会读

if (COMP.compare(anArray[j],anArray[i]) < 1) {

Here's a solution for your last question. 这是您上一个问题的解决方案。 Create two overloaded static methods, one with a Comparator, one without, then also create a Comparator that uses the natural order (amazingly no such thing is available to my knowledge in the JDK). 创建两个重载的静态方法,一个使用Comparator,一个不使用,然后还创建一个使用自然顺序的Comparator(令人惊讶的是,我在JDK中没有这样的东西可用)。 Something like this: 像这样的东西:

private static final Comparator<? extends Comparable> NATURAL_ORDER = new Comparator<Comparable>() {
    @Override
    public int compare(final Comparable o1, final Comparable o2) {
        return o1.compareTo(o2);
    }
};

private static <T> Comparator<T> naturalOrder() {
    return (Comparator<T>) NATURAL_ORDER;
}

public static <T> void sort(final T[] array) {
    if (!Comparable.class.isAssignableFrom(array.getClass().getComponentType())) {
        throw new IllegalArgumentException(
              "Array Component Type must implement Comparable");
    }

    sort(array, naturalOrder());
}

public static <T> void sort(final T[] array, final Comparator<? super T> comparator) {
    // implement sort here
}

You can use Arrays.sort 您可以使用Arrays.sort

Arrays.sort(myArray, myComparator)

Then you can pass in any comparator you want. 然后你可以传入你想要的任何比较器。

You can create instance of SelectionSort to use it for comparison 您可以创建SelectionSort实例以将其用于比较

SelectionSort selectionSort = new SelectionSort();
if (selectionSort.compare(anArray[i], anArray[j]) < 1) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM