zoukankan      html  css  js  c++  java
  • 遍历枚举里的所有值

    1.编写枚举类

    假设枚举类如下,现在需要遍历里面的deptId

    public enum AreaEnum {
    
        SHANDONG(1L, new BigDecimal(101527453), new BigDecimal(157900)),
        SHANGHAI(2L, new BigDecimal(24180000), new BigDecimal(6340.5)),
        JIANGXI(3L, new BigDecimal(45188635), new BigDecimal(166900)),
        SICHUAN(4L, new BigDecimal(83674866), new BigDecimal(486000));
    
        private Long deptId;
    
        private BigDecimal people;
    
        private BigDecimal area;
        
        public Long getDeptId() {
            return deptId;
        }
    
        public BigDecimal getPeople() {
            return people;
        }
    
        public BigDecimal getArea() {
            return area;
        }
    
        AreaEnum(Long deptId, BigDecimal people, BigDecimal area) {
            this.deptId = deptId;
            this.people = people;
            this.area = area;
        }
    }

    2.遍历枚举类

    在枚举类里添加如下静态方法即可遍历

        public static AreaEnum getByDeptId(Long deptId) {
            for (AreaEnum status : AreaEnum.values()) {
                if (status.getDeptId().equals(deptId)) {
                    return status;
                }
            }
            return null;
        }

    注:返回类型是枚举

    一点点学习,一丝丝进步。不懈怠,才不会被时代所淘汰!

  • 相关阅读:
    attr 修改IMG src
    64转2
    :eq
    DOM0和D0M2级事件
    c's's透明度
    span标签 宽度无效解决方案
    CSS 点击事件
    input点击后的 默认边框去除
    js
    CSS 的 ID 和 Class 有什么区别,如何正确使用它们。
  • 原文地址:https://www.cnblogs.com/fqh2020/p/15543508.html
Copyright © 2011-2022 走看看