It is not illegal because in Java (and other languages), the char in this case will be casted to an int. Such as (int)'%'
Something like this would be safer if you don't know what type of char value it will be.
int[][] myArray = new int[Character.MAX_VALUE][Character.MAX_VALUE];
myArray['%']['^'] = 24;
It will work since Java will translate that to:
myArray[37][94] = 24;
Make sure you place max value of char (as already done) which is basically, '?' which translates to 65535. Or you will get an ArrayOutOfBoundsException.
If you know your range, you can still use 256. Just make sure you do proper bounds checking where the input is >=0 and < 256.
public int getItem(int x, int y) {
if ((x < 0 && x > 255) || (y < 0 && y > 255)) {
return -1; // Or you can throw an exception such as InvalidParameterException
}
return myArray[x, y];
}
Error bounds checking plays an important role for this.
I would rethink what your trying to do, something like this is unreadable, what are you trying to achieve? Perhaps you can use a different collections?
EDIT
Changed the types since you changed the code.