This is not illegal, because in Java (and other languages) char in this case will be passed to int. For example, (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 as Java translates this:
myArray[37][94] = 24;
Make sure you put the maximum value from char (as already done), which is basically '?' which corresponds to 65535. Or you get an ArrayOutOfBoundsException .
If you know your range, you can still use 256. Just make sure you do the correct bounds checks, 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;
Checking the boundaries of errors plays an important role for this.
I would rethink what you are trying to do, something like this is unreadable, what are you trying to achieve? Perhaps you can use different collections ?
EDIT
Types changed since you changed the code.
source share