【问题标题】:Create an array in which every position is an enum type创建一个数组,其中每个位置都是枚举类型
【发布时间】:2012-01-18 01:53:26
【问题描述】:

您好,我想创建一个数组,其中每个位置都由一个枚举值表示。

例如:

public enum type{

  green,blue,black,gray;

}

现在我想创建一个数组,其中每个位置都是绿色、蓝色、...

我会更清楚。我想创建一个数组,其中位置由枚举类的值表示。而不是 int[] array = new int[10] create int[] array = new int[type.value]

【问题讨论】:

  • 你想用颜色给你的数组编号吗?
  • 我不认为这是可能的;数组需要知道它们的大小,而枚举——虽然在内部处理得有点像数字——不是数字,所以不能从中得出大小。
  • @G.Bach 好的,我会知道的
  • 你能介绍一下你的用例吗(你想用这个数组做什么)?

标签: java arrays enums


【解决方案1】:

枚举是 java.lang.Enum 的扩展,因此您可以使用 java.lang.Enum 的方法将枚举用作数组的维度。

枚举.values().length 呈现枚举的大小;

enumValueordinal() 呈现枚举中某个值的索引。

例如:

public class thisClass {
    enum thisEnum = {valA, valB, valC, valD, valE }
    private int[] arrayForThisEnum;

    public thisClass() {
        this.arrayForThisEnum = new int[thisEnum.values().length];
        // further initialization
    }

    public void addOne(thisEnum xxx) {
        this.arrayForThisEnum[xxx.ordinal()]++;
    }
}

【讨论】:

    【解决方案2】:

    如果我错了,请先纠正我:您试图将每种颜色与 int 值相关联。

    如果是这样,您正在寻找的是associative array,它在Java 中被建模为Map。所以你可以使用一些Map<type, Integer> 来实现你想要的(最好是一个EnumMap,它针对使用Enum 键进行了优化)。

    // I renamed your "type" to Color
    Map<Color, Integer> map = new EnumMap<Color, Integer>(Color.class);
    map.put(Color.BLUE, 3);
    

    然而,如果你真的想使用一个数组,你可以使用枚举常量的ordinal() 方法(它返回一个int 代表常量在枚举声明中的位置,从0开始):

    int[] ints = new int[Color.values().length];
    ints[Color.BLUE.ordinal()] = 3;
    

    如果此关联对于您的整个应用程序是唯一的(如果您永远不需要同时将一种颜色与多个值关联;换句话说,如果某些客户端存储BLUE --&gt; 2和其他一些商店BLUE --&gt; 3),那么最好将该值存储在枚举本身中:

    enum Color {
        GREEN, BLUE, BLACK, GRAY;
    
        private int value;
    
        // ...getter and setter for value...
    
    }
    

    然后你可以写:

    Color.BLUE.setValue(8);
    

    对于读取值:

    for (Color c : Color.values()) {
        System.out.println("There are " + c.getValue() + " occurences of " + c);
    }
    

    【讨论】:

      【解决方案3】:

      它是type[] allValues = type.values()。见this question

      您也可以使用EnumSet:

      EnumSet<type> types = EnumSet.allOf(type.class);
      

      它为您提供了高性能的Set 实现,其中填充了您的枚举值。

      PS:你应该以大字母(CamelCase)开头的枚举类命名。

      编辑:

      似乎你想要你的emum值的ordinal位置数组(为什么现在有人会使用数组而不是正确的集合?):

      type[] colors = type.values();
      List<Integer> list = new ArrayList<Integer>(colors.length);
      for (type color : colors) {
        list.add(color.ordinal());
      }
      Integer[] array = list.toArray(new Integer[0]);
      

      EDIT2:也许您希望 Map&lt;Integer, type&gt; 具有类似 0 =&gt; green, 1 =&gt; blue, 2 =&gt; black, 3=&gt; gray 的键和值(问题仍然不清楚)?

      【讨论】:

        【解决方案4】:

        您想要一个type 对象数组吗?您可以拨打type[] types = type.values()

        【讨论】:

          【解决方案5】:

          这应该可行:

           MyColor[] myList;
          

          然后

           myList = new MyColor[20];
          

          nb,我不会使用type 作为枚举的名称,因为它没有很好的含义。像颜色这样的东西更好。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-01-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-10-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多