Y
Y
Yaten2022-02-12 13:38:30
Java
Yaten, 2022-02-12 13:38:30

How to access enam in numerical order?

there is this standard enam:

public enum DayOfWeek {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY;

    DayOfWeek() {
    }
}


Is it possible, when referring to one of the days, to enter a serial number ("0" for example) to display MONDAY?
I can’t find the conversion rule, I don’t remember how to write it down correctly, so that it would work,

thanks for your attention,

if necessary, I can show the code where I enter / output the days of the week, and why I need it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Roo, 2022-02-12
@Ya10

I would make another field with a getter:

public enum DayOfWeek {
    MONDAY(1),
    TUESDAY(2),
    WEDNESDAY(3),
    THURSDAY(4),
    FRIDAY(5),
    SATURDAY(6),
    SUNDAY(7);

     @Getter
     private final int number;

    DayOfWeek(int number) {
        this.number = number;
    }
}

M
Michael, 2022-02-12
@Akela_wolf

DayOfWeek.values()[index]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question