int[] or List<Integer>?
Hi there! I'm having some trouble deciding on what to do with this, so I'd appreciate some help. The problem is this: I'm writing a program that encodes/decodes messages into/from series of bits (you code 'a' as 010011, decode 010011 as 'a'). For this, I had aHashMap<Character, int[]> for the coding and aHashMap<int[], Character> for the decoding, but it wouldn't work becauseint[] doesn't work for the key searches on the map (the decoding map). So I changedint[] toArrayList<Integer>.
And this is where the problem arises: for the coding and decoding processes it was quite convenient to have the codes asint[], since I have multiply this codes by matrices (which are currentlyint[][]), etc, and it's more immediate to do so withint[].
Changing the matrices toList<List><Integer>> would make the code a mess (in my opinion), since instead of doingm[j][k], I'd have to dom.get(j).get(k), etc, which isn't *that* bad, but I don't like it.
On the other hand, if I don't change them toList<List><Integer>> I'm facing a code in which items of apparently the same nature have different types.
So, what would you do in my place? Would you change them toList<List><Integer>>? Is there any other way?
Thanks a lot.
Sorry about my English :P
> How many ints make up one char according to your codec
> algorithm?
It's up to the user.
> Encapsulate the array of integers in a class that is
> more meaningful, and provide that class with equals
> and hashCode methods. Then use this class instead of
> an array in the Map.
Yeah, I'd thought about that, but wasn't especially fond of the idea, among other things because in Java you can't redefine operators, so I can't use operator[] anymore with my matrices. I can't, anyway, if I use List<Integer>, but... Dunno. Thank you, in any case!
I'll probably end up doing this, but I'd still like to hear other opinions.
> Yeah, I'd thought about that, but wasn't especially
> fond of the idea, among other things because in Java
> you can't redefine operators, so I can't use
> operator[] anymore with my matrices. I can't, anyway,
> if I use List<Integer>, but... Dunno.
You can expose the array through a public variable or an accessor method.
This means that the class won't be mutable anymore so you'll have to be careful when you use it as a map key.
> Hi there! I'm having some trouble deciding on what to
> do with this, so I'd appreciate some help. The
> problem is this: I'm writing a program that
> encodes/decodes messages into/from series of bits
> (you code 'a' as 010011, decode 010011 as 'a'). For
> this, I had a HashMap<Character, int[]>
Based entirely on the data you provided then I would use a array - nothing else.
If your character set must deal with one of the bigger sets that might not be practical but even then it might be.
It is much faster than any other method.