I have a lookup table that should be accessed by two separate key values. One ugly way to do this is:
int[][] myArray = new int[256][256];
myArray[key1][key2] = 25;
where key1 and key2 are keys that were previously generated dynamically. But this is rather ugly. It seems that a better way to do this would be to use a Map, but those require a single key, not two. Java doesn’t natively support tuples, so what should I use instead? (It also seems awkward to use an array as the key).
EDIT: The reason I say that this isn’t particularly pretty is that my array is actually referenced by character values, not integers. They could be used interchangeably, but the responses to my previous question seem to suggest otherwise:
What’s so ugly about that? That’s about as simple as 2D matrices can be in Java, and it’s quick, too.
If you really want to use a map, just defined your own
Tupleclass to use as a key – but be sure that you overrideequals()andhashCode()correctly! I’d recommend implementing an immutableTupleclass because using mutable objects as map keys can cause serious issues.Tuple.java