Java枚举按数字范围搜索 [英] Java enum search by number range

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

问题描述

是否可以像下面那样执行枚举

 枚举{

10差
$ 100
1000 >所以当我通过输入值搜索时,让我们说101.它会返回富吗?在枚举中怎么做?可以举个例子吗我不想用forloop循环整个枚举来获取string_value。可能?

解决方案

使用枚举已经建议。



然后,不是通过枚举值执行强力迭代搜索,而是提供一个静态的 lookup(int)方法,通过所有值的有序列表/数组执行二进制搜索



要执行搜索,请以中间值或中间值开头为root,并将我们要查找的值与该值进行比较。



如果我们正在寻找的价值是这样,那么我们完成了。如果不到,那么我们从中间开始搜索下半部分的值。如果更大,那么与它之后的值进行比较只是为了看它是否在范围内。如果仍然较大,则搜索上半部分,依此类推。






编辑:根据要求提供代码示例。 / p>

 公开枚举财富{

BROKE(0),
DESTITUTE(10),
POOR(100),
MIDDLE_CLASS(10000),
RICH(100000),
MILLIONAIRE(1000000),
BILLIONAIRE(1000000000);

private final int value;

private Wealth(final int value){
this.value = value;
}

public final int getValue(){
返回值;
}

/ **
* @param v
*我们正在寻找的价值
* @return财富
* /
public static Wealth lookup(final int v){
final Wealth [] a = Wealth.values();
int min = 0;
int max = a.length - 1;
int i;
do {
i =(min + max)/ 2;
final int av = a [i] .value;
if(v< av){
max = i;
} else if(v> av){
if(i + 1< a.length&& v< a [i + 1] .value){
break ;
}
min = i + 1;
}
} while(v!= a [i] .value&& min< max);
if(min == max){
return a [max];
}
return a [i];
}

}

几个注释:



这假设 Wealth 的值已经被订购了。否则,一个快速排序(双关语!)应该做的伎俩。



这可能不是最有效的实现,只是一个快速,维基百科上的代码。



如果您的数量少于十几个值,则线性搜索可能仍然会更有效(代码肯定更为自明)比二进制搜索。二进制搜索只有当您拥有数十或数百种价值观时才真正付出代价,并执行数百万次查询。



鉴于您的原始值,这是非常早期的优化。我只是想为那些正在使用大量价值观的人提供一个选择。


Is it possible to do enum like below

enum {

 10  poor
 100  rich
 1000 very_rich


}

so that when i do search by input value let say 101. it will return "rich" ? how to do this in enum? can give example? i do not want to loop entire enum with forloop to get the string_value. possible?

解决方案

Use an enum with values, as the others have already suggested.

Then, instead of performing a brute-force iterative search through the enum values, provide a static lookup(int) method that performs a binary search through an ordered list/array of all the values.

To perform the search, start with the median or middle value as 'root', and compare the value we're looking for with that.

If the value we're looking for is exactly that, then we're done. If it's less than that, then we start searching the lower half of the values, again starting from the middle. If greater, then compare with the value right after it just to see if it falls in range. If it's still larger, then search in the upper half, and so on.


EDIT: Code sample as requested.

public enum Wealth {

    BROKE(0),
    DESTITUTE(10),
    POOR(100),
    MIDDLE_CLASS(10000),
    RICH(100000),
    MILLIONAIRE(1000000),
    BILLIONAIRE(1000000000);

    private final int value;

    private Wealth(final int value) {
        this.value = value;
    }

    public final int getValue() {
        return value;
    }

    /**
     * @param v
     *        the value we're looking for
     * @return Wealth
     */
    public static Wealth lookup(final int v) {
        final Wealth[] a = Wealth.values();
        int min = 0;
        int max = a.length  - 1;
        int i;
        do {
            i = (min + max) / 2;
            final int av = a[i].value;
            if (v < av) {
                max = i;
            } else if (v > av) {
                if (i + 1 < a.length && v < a[i + 1].value) {
                    break;
                }
                min = i + 1;
            }
        } while (v != a[i].value && min < max);
        if (min == max) {
            return a[max];
        }
        return a[i];
    }

}

Several notes:

This assumes that the values for Wealth are already ordered. Otherwise, a quick sort (pun!) should do the trick.

This probably isn't the most efficient implementation, just a quick and dirty one adapted from the pseudo-code on Wikipedia.

If you have fewer than, say, a dozen values, then a linear search might still be more efficient (and the code definitely more self-explanatory) than a binary search. Binary search only really pays off when you have dozens or hundreds of values, and you perform lookups millions of times.

Given your original values, this is evil, premature optimization. I just wanted to bring it up as an option for those who're working with large sets of values.

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

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