Java rarrange枚举数组 [英] Java rarrange enum array

查看:168
本文介绍了Java rarrange枚举数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何重新排序enum,以便所有山羊都在开始,所有的绵羊都在阵列的末尾。现在它实际上是诀窍,但直到数组大小> 100 ..重新排序速度也很重要所以api方法有点太慢。有什么建议吗?

i was wondering how can i reorder enum so that all goats are at the beginning and all sheep are at the end of the array. Right now it actually does the trick but until the array size > 100.. The reordering speed also matters so api methods are bit too slow. Any suggestions?

public class Sheep {


   enum Animal {sheep, goat};

   public static void main (String[] param) {

    reorder(Animal.values());
   }

   public static void reorder (Animal[] animals) {


       int l, r, i, j;

       i = l = 0; //left most element
       r = animals.length - 1;//right most element
       int mid = (r+l)/2; // middle element of the array
        for(i=0; i < animals.length;i++)
        {
            if(i < mid)
            {
                animals[i] = animals[l+1];

                System.out.println(animals[r]);

            } else if(i >= mid )
            {   
                animals[i] = animals[r-1];
                System.out.println(animals[r]);

            }

        }

   }
}


推荐答案

由于 enum 实现可比较,你可以简单地排序然后反转数组:

Since an enum implements Comparable, you can simply sort and then reverse the array:

public static void reorder(Animal[] animals) {
    Arrays.sort(animals);
    for (int i = 0, j = animals.length - 1; i < j; ++i, --j) {
        Animal tmp = animals[i];
        animals[i] = animals[j];
        animals[j] = tmp;
    }
}

您也可以这样做:

List<Animal> list = Arrays.asList(animals);
Collections.sort(list);
Collections.reverse(list);

这基本上与API调用相同,其中包含数组的(非常轻微的)开销一个列表对象。你甚至可以这样做:

This basically does the same thing with API calls with the (very slight) overhead of wrapping the array in a List object. You can even do this:

Arrays.sort(animals, Collections.reverseOrder());

(感谢Bhesh Gurung的想法。)

(Thanks to Bhesh Gurung for the idea.)

编辑:如果你必须处理两个值,你可以通过简单地从两端扫描来做得更好,交换时你会发现两个元素无序:

If you have to deal with exactly two values, you can do much better by simply scanning from both ends, swapping as you find two elements out of order:

public static void reorder(Animal[] animals) {
    int first = 0;
    int last = animals.length - 1;
    while (first < last) {
        /*
         * The unsorted elements are in positions first..last (inclusive).
         * Everything before first is the higher animal; everything after
         * last is the lower animal.
         */
        while (animals[first].ordinal() == 1 && first < last) {
            ++first;
        }
        while (animals[last].ordinal() == 0 && first < last) {
            --last;
        }
        if (first < last) {
            /*
             * At this point, the sort conditions still hold and also we know
             * that the animals at first and last are both out of order
             */
            Animal temp = animals[first];
            animals[first] = animals[last];
            animals[last] = temp;
            ++first;
            --last;
        }
    }
}

但是,如果您只需要要做的是生成正确的输出(而不是实际排序数组),然后@ajb在评论中建议的方法是最好的:只计算有多少只绵羊和山羊,并多次打印相应的值。

However, if all you need to do is generate the right output (and not actually sort the array), then the approach suggested by @ajb in a comment is the best: just count how many sheep and goats there are and print the corresponding values that many times.

这篇关于Java rarrange枚举数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆