Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6541943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:09:58+00:00 2026-05-25T11:09:58+00:00

Is it possible to do enum like below enum { 10 poor 100 rich

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:09:59+00:00Added an answer on May 25, 2026 at 11:09 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I like to use enum as value holders whereever possible and I like it.
It seems to be possible in Java to write something like this: private enum
I would like to lookup an enum from its string value (or possibly any
Possible Duplicate: How do I serialize an enum value as an int? Hi, all!
Possible Duplicate: How do I enumerate an enum? Say I have an enum type
Is it possible to have an enum change its value (from inside itself)? Maybe
I have an enum(below) that I want to be able to use a LINQ
Possible Duplicate: Using GetHashCode for getting Enum int value Is it safe to use
I have an enum that maps to possible field values in a char(1) field
I have some C++ code that looks like this: enum { FOO = 0x01,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.