How to reverse enum?

前端 未结 2 1655
不知归路
不知归路 2021-01-13 16:51

I have an Enum class below

public class PTalkCommand {
public enum Code {
        CLR((byte) 0),
        ACK((byte) 170),
        SER((byte) 0),
        NAK         


        
2条回答
  •  梦谈多话
    2021-01-13 17:35

    There's a built-in .values() method that returns an array of all the enum constants. You can iterate it backwards.

    Code[] values = Code.values();
    for (int i = values.length - 1; i >= 0; i--) {
        Code next = values[i];
        //do your thing
    }
    

提交回复
热议问题