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);
        }
    }
}

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

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

相关推荐
Vallelonga2 分钟前
C++ 中的智能指针
开发语言·c++
探索java3 分钟前
Spring Boot自定义Starter:从原理到实战全解析
java·spring boot·后端
阿巴~阿巴~27 分钟前
深入解析C++流运算符(>>和<<)重载:为何必须使用全局函数与友元机制
开发语言·c++
码破苍穹ovo44 分钟前
堆----3.数据流的中位数
java·数据结构·算法·力扣
一只 Lemon1 小时前
PHP-单引号和双引号(通俗易懂讲解版)
开发语言·php
忧郁的蛋~1 小时前
C#中LINQ to DataSet操作及DataTable与LINQ相互转换
开发语言·c#·linq
呼哧呼哧.2 小时前
Java 8特性(一)
java·开发语言
Runing_WoNiu2 小时前
Golang 与Java 单例模式、工厂模式比较
java·单例模式·golang
Armyyyyy丶3 小时前
Sentinel原理之责任链详解
java·sentinel·熔断限流
胡萝卜的兔3 小时前
go语言标准库学习, fmt标准输出,Time 时间,Flag,Log日志,Strconv
开发语言·学习·golang