0

I'm developing an Android application:

I have this Enum:

public enum Gender
{
    male (0, MyApplication.getAppContext().getString(R.string.male)),
    female (1, MyApplication.getAppContext().getString(R.string.female));

    private final int gender;
    private final String description;
    Gender(int aGender, String aDescription)
    {
        gender = aGender;
        description = aDescription;
    }

    public int getValue() { return gender; }

    @Override
    public String toString() { return description; }

    /**
     * Returns all values in this Enum sorted alphabetically by
     * description.
     * @return All values sorted.
     */
    public static Gender[] getSortedVaules()
    {
        Gender[] sorted = values();
        Arrays.sort(sorted, EnumByNameComparator.INSTANCE);
        return sorted;
    }
}

Imagine I have int gender = 1. In this case, 1 is the value for Gender.female.

I want to use gender variable to get the index of Gender.female enum in the array returned by Gender.getSortedValues().

I think I have to use gender variable to get an its Gender representation, in other words, to get an enum variable with Gender.female as value. And then, use that enum variable to search on Gender.getSortedValues(). But I don't know how to get an enum using its value.

How can I do that?

VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • Have you tried and compiled this code? I am not sure it compiles due to `Arrays.sort()`: the `values()` array should not be modifiable, normally – fge Jul 13 '13 at 09:34
  • My code works and compiles perfectly. – VansFannel Jul 13 '13 at 09:35
  • OK, that's strange... I guess `.values()` makes a copy then. – fge Jul 13 '13 at 09:35
  • [Suggestion](http://pastebin.com/figq0JvN) to [your now deleted question](http://stackoverflow.com/questions/17630080/store-static-method-result-in-a-static-variable). BTW don't delete question just because someone downvoted it. – Pshemo Jul 13 '13 at 12:29
  • Also why didn't you accept fge answer if it solves your problem and you are using it? – Pshemo Jul 13 '13 at 12:35
  • @Pshemo Also, are you sure I'm using fge answer? Why don't you have a litte more patience? – VansFannel Jul 14 '13 at 05:27
  • @VansFannel How I know you are using fge answer? Take a look [here](http://pastebin.com/i0PeqtR0). Only difference is that you corrected it and returning `g` not `gender`. I also don't get your comment about patience. What makes you think I don't have it :) ? – Pshemo Jul 14 '13 at 10:29

2 Answers2

3

If you have an integer which is the gender member of this enum, you should add a static method to your enum:

public static Gender getByInt(final int i)
{
    for (final Gender g: values())
        if (g.gender == i)
            return g;
    return null; // not found
}

You'll then call Gender.getByInt(...);.

But Enum also has .valueOf(): Enum.valueOf(Gender.class, "male") or Gender.valueOf("male") for instance; beware that it throws an IllegalArgumentException if there is no such value.

Also read about .ordinal(). Javadoc for Enum.


final note: naming your int gender is confusing.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
fge
  • 119,121
  • 33
  • 254
  • 329
0

Remove the member field gender. You introduce a potentially non unique field in an already perfect ordinal type, which can lead to programming bugs later on.

Then just rely on the ordinal() method, when you have to serialize the enum somewhere. Use a loop and identity comparison for the lookup (as suggested by fge).

For the sorted values, use an immutable static list (or other indexed collection) instead of an array, because then you can use list.indexOf(myEnum) easily and have to sort only once.

Bananeweizen
  • 21,797
  • 8
  • 68
  • 88
  • If I remove the member field gender. What do I have to use to store values 0, 1, ...? – VansFannel Jul 13 '13 at 10:00
  • http://stackoverflow.com/a/5021384/44089 The enum itself already has this by its ordinal() method. You only need to store an own index like you have above if the elements in the enum may change over time (i.e. if you have an enum defining car brands or the like). But for a gender enumeration I cannot really imagine that. :) – Bananeweizen Jul 13 '13 at 10:07
  • In my experience relying on the ordinal() is a bad practice. Imagine a developer adding an enum element before or in the middle of other members. Explicitly assigning an identifier field is, in many cases, a good practice (e.g. when storing on the database that value to represent the enum for an entity) – dkateros Jul 13 '13 at 10:22