I want to initialize a multidimensional private static final array of ints, indexing the values.
private static final int[][] a = { {0,0,0}, {1,2,3} };
This is NOT good for me. I found somewhere this weird syntax that I tried, but does not want to compile in anyway. I add it to clarify what I need:
private static final int[][] a;
private static {
a = new int[NUM_TYPES][3];
a [TYPE_EMPTY] = { 0, 0, 0 };
a [TYPE_NORMAL] = { 1, 2, 3 };
};
The difference is that now I should have a[TYPE_EMPTY] and a[TYPE_NORMAL] instead of a[0] and a[1]. On the practical side it's the same, but the second one makes much more clear, error-free and maintainable the source.
For example, should I add a new TYPE in future, I would not need to care what numerical index would have inside the array.
As I said, I did not find any correct syntax to do that, and the above syntax is completely wrong. Would some Java expert give me a short lesson? :) Thank you very much.
NUM_TYPESis fixed to be2, then you can simply usea[0]anda[1]. But what's the problem with first case?HashMaphere, since you are relating the arrays with some types.HashMaprather than defining constants for those types, and using them as indices. Other possibility is to use anTypeenum, and add types to it. And then use aHashMap<Type, Integer[]>. But I'm simply shooting in the dark, taking lots of assumptions, just on the basis of what I'm seeing.