There is such a class with Enum:

public class Segment { private SegmentValue value; //-----===== getter и setter всех полей ====----- public SegmentValue getValue() { return value; } public void setValue(SegmentValue value) { this.value = value; } public static enum Age implements SegmentValue{ AGE1("-18"), AGE2("19-24"), AGE3("25-34"), AGE4("35-44"), AGE5("45+"); private String value; Age(String value) { this.value = value; } public String toString() { return value; } } 

How do I get an enum value by index? Let me explain: you need to generate a random object from Enuma Age. Generating a random number from 0 to 4 is not a problem. The problem is how to return the value of enum.

    4 answers 4

    If you can generate a random number from 0 to 4, what's the problem?

     Age[] ages = Age.values(); Age randomAge = ages[randInt]; //randInt - случайное число 

      The solution is simple, first: enter two more fields in Enum:

       public static enum Age implements SegmentValue{ AGE1("-18", 0, 18), AGE2("19-24", 19, 24), AGE3("25-34", 25, 34), AGE4("35-44", 35, 44), AGE5("45+", 45, Integer.MAX_VALUE); private String value; private int minAge; private int maxAge; Age(String value, int minAge, int maxAge) { this.value = value; this.minAge = minAge; this.maxAge = maxAge; } 

      The solution is more difficult - to parse value.

      Well, and then that in the first cases that in the second cycle to sort:

       public Age getAge(int param/String param){//тут все зависит от входногопараметра for(Age age : Age.values()){ if(условиена вхождение в интервал){ return age; } } } 
      • I liked the first decision, only I did not fully understand it - first, why introduce an additional 2 values ​​to enums? And you can not do without the assignment of values? And without going through the loop? - Stas0n
      • because you have a condition, if in the first case, then if (param> = age.minValue && param <= age.maxValue) {return age; } Maybe you can write something better, but a bad one is better, but it works better than a good one that doesn’t work :) - Anton Feoktistov

      Do not want to enter additional parameters, you can parse the string yourself:

       public class TestEnum { enum AgeRange { AGE1 ( "-18" ), AGE2 ( "19-24" ), AGE3 ( "25-34" ), AGE4 ( "35-44" ), AGE5 ( "45+" ) ; private String value ; private int lower, upper ; private AgeRange ( String value ) { this.value = value ; this.lower = parseLower ( value ) ; this.upper = parseUpper ( value ) ; } private int parseUpper ( String value ) { // TODO implement return 0 ; } private int parseLower ( String value ) { // TODO implement return 0 ; } private boolean isInRange ( int age ) { return this.lower <= age && age <= this.upper ; } public String toString () { return value ; } public static AgeRange valueOf ( int age ) { for ( AgeRange item : values () ) { if ( item.isInRange ( age ) ) { // item found return item ; } } return null ; } } public static void main ( String[] args ) { for ( int i = 0 ; i < 99 ; i++ ) { System.out.println ( i + " / " + AgeRange.valueOf ( i ) ) ; } } } 

        For example, you can enter in the enum intovye ID, unique for each element (increment from 0). And finally read at least some book on Java ...

        • and how to enter the final ID directly into the enum? - Stas0n
        • Not final, but ints. Like this: public static enum Age implements SegmentValue {private static int maxid = 0; private int id; Age (String value) {this.id = this.maxid ++; this.value = value; } - user6550
        • And how then to turn to the enum, say, under the symbol 2? All of them will get the same ID - 0 I changed private to public and brought this out: print.n (Segment.Age.AGE4.id); - only zeros turned out - Stas0n