Re: can you help me in java
Lew wrote:
Oliver Wong wrote:
public static char convertToArabicDigit(int i) {
** snip **
Joshua Cranmer wrote:
Or the much shorter variant (w/o checking):
return 0x660 + i;
You'd need to downcast that to char, and presumably mask or error-check
the value of 'i'.
The switch variant is longer, but that is not important. It's safer
and, chances are, faster. And it's only significantly longer in the
source code, where length is not harmful.
A clever approach might use a Map< Integer, Character>. "Clever" !=
"good". ("Clever" != "bad", but "clever" == "risky".)
Switches are not necessarily faster: take converting '0' to 0:
The probable fastest is way is (char)(c & 0xf) - it is a simple bit
mask. A switch, by contrast, is probably at best going to calculate an
offset and then load a value to return.
Changing numeric numbers by manipulating the relative zero character
makes the most sense to me, because of the way Unicode treats digits. It
is easiest for me to see '(Farsi 0)' + i and recognize what it does as
opposed to a switch with '(Farsi 0)' ... '(Farsi 9)', simply because
that's how I process information.
To each his own, I guess.