Java:数据结构-枚举

枚举

概念:将一组常量组织起来,在这之前表示一组常量通常使用定义常量的方式。

java 复制代码
public static  final int RED = 1;
 public static  final int GREEN = 2;
 public static  final int BLACK = 3;

枚举的使用

java 复制代码
public enum enumTest {
    RED(0,"RED"),GREEN(1,"GREEN"),BLUE(2,"BLUE"),
    BLACK(3,"BLACK"),WHITE(4,"WHITE");
    public int ordinal;
    public String color;

    enumTest(int ordinal, String color) {
        this.ordinal = ordinal;
        this.color = color;
    }
    public static void main1(String[] args) {
        enumTest enumTest=RED;
        switch (enumTest){
            case RED -> {
                System.out.println("RED");
                break;
            }
            case BLACK -> {
                System.out.println("BLACK");
                break;
            }
            case BLUE -> {
                System.out.println("BLUE");
                break;
            }
            case WHITE -> {
                System.out.println("WHITE");
                break;
            }
            default -> {
                System.out.println("无法匹配");
                break;
            }
        }
    }
}

枚举的常用方法

|-------------|------------------|
| values() | 以数组形式返回枚举类型的所有成员 |
| ordinal() | 获取枚举成员的索引位置 |
| valueOf() | 将普通字符串转换为枚举实例 |
| compareTo() | 比较两个枚举成员在定义时的顺序 |

java 复制代码
public static void main(String[] args) {
        TestEnum[] testEnums = TestEnum.values();
        for (int i = 0; i < testEnums.length; i++) {
            System.out.println(testEnums[i] +" " +testEnums[i].ordinal());
        }

        TestEnum testEnum = TestEnum.valueOf("WHITE");
        System.out.println(testEnum);

        System.out.println(WHITE.compareTo(BLACK));
    }

枚举通过反射来拿到实例的对象

java 复制代码
public class Test1 {
    public static void main(String[] args) {
        try {
            Class<?> c1 = Class.forName("enumdemo.enumTest");
            Constructor<?> constructor = c1.getDeclaredConstructor
                    (String.class, int.class, int.class, String.class);
            constructor.setAccessible(true);
            enumTest enumTest = (enumTest) constructor.newInstance(9, "YELLOW");
            System.out.println(enumTest);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

注:枚举本身就是一个类,其构造方法默认为私有的,枚举可以避免反射和序列化问题。

希望能对大家有所帮助!!!

相关推荐
RelishCoding1 分钟前
02-二分查找
数据结构
闻哥2 分钟前
Java虚拟机内存结构深度解析:从底层原理到实战调优
java·开发语言·jvm·python·面试·springboot
茶本无香2 分钟前
【无标题】
java·设计模式·策略模式
wjs20243 分钟前
HTML 属性详解
开发语言
无巧不成书02186 分钟前
Kotlin Multiplatform (KMP) 鸿蒙开发整合实战|2026最新方案
android·开发语言·kotlin·harmonyos·kmp
wangbing112510 分钟前
平台介绍-SDK包
java
非得登录才能看吗?11 分钟前
Qt 的cmake与qmake
开发语言·qt
仰泳之鹅15 分钟前
【FreeRTOS】调试技巧篇
开发语言
亓才孓22 分钟前
[SpringIOC]NoUniqueBeanDefinitionException
java·spring
量子炒饭大师23 分钟前
【C++入门】Cyber高维的蜂巢意识 —— 【类与对象】static 成员
开发语言·c++·静态成员变量·static成员