Please first correct me if I'm wrong: you're trying to associate each color with an int
value.
If so, what you're looking for is an associative array, which in Java is modelled as a Map
. So you can use some Map<type, Integer>
to achieve what you want (preferably an EnumMap
which is optimized for using Enum
keys).
// I renamed your "type" to Color
Map<Color, Integer> map = new EnumMap<Color, Integer>(Color.class);
map.put(Color.BLUE, 3);
However, if you really want to use an array, you can make use of the ordinal()
method of your enum constants (which returns an int
representing the constant's position in the enum declaration, starting from 0):
int[] ints = new int[Color.values().length];
ints[Color.BLUE.ordinal()] = 3;
If this association is meant to be unique for your entire application (if you never need to associate a color with more than one value at the same time; in other words, if it's never the case that some client stores BLUE --> 2
and some other stores BLUE --> 3
), then it would be better to store that value in the enum itself:
enum Color {
GREEN, BLUE, BLACK, GRAY;
private int value;
// ...getter and setter for value...
}
Then you can write:
Color.BLUE.setValue(8);
And for reading the values:
for (Color c : Color.values()) {
System.out.println("There are " + c.getValue() + " occurences of " + c);
}