NamHo Lee
NamHo Lee

Reputation: 329

making an array with the length of chars in unicode

I am writing a code in java , where every time a user types 1 character it checks if it is one of the special characters

char a = getFromInput();

if(a = '$'){
  //do something

}else if(a = '@'){
  //do something


}else if(a = '#'){
  //do something


}else if(a ='~'){
  //do something


}else if(a ='^'){
  //do something


}else if(a ='&'){
  //do something


}... // the if statements go on 

However It came to me that this is inefficient since it uses to much if statements . To solve this, I came up with the idea to make an array of bytes with the length of the unicode index of the last special character

byte[] bytes = new byte[1000] 

// the length 1000 is because I thought most of the special characters are 
// index of less that 1000 in unicode UTF-8 mapping

Functor[] functors;

// functors are just objects that i made that contain function

char a = getFromInput();
if(byte[a] >100){
   // do nothing when byte[char] is larger that 100 
}else {
    functors[byte[a]].func();
}

So basically what i'm trying to do is using the char variable as index of the array. What i'm curious is

  1. Would using more that 100 if statements for every character the user inputs be okay? are there any suggestions ?

  2. I know making an array would cost more memory but would it be better than using multiple (tons of ) if statement with respect to the performance ?

Upvotes: 0

Views: 69

Answers (2)

Stephen C
Stephen C

Reputation: 719307

Would using more that 100 if statements for every character the user inputs be okay? are there any suggestions ?

It is up to you to decide what is "okay" ... unless you provide us with the criteria.

However, there are a variety of other alternatives to lots of if statements:

  • a switch statement
  • if statements that test character ranges rather than individual characters
  • an array of SomeFunction where the character is the array index
  • an array of codes (indexed by character) that maps characters to code values (e.g. SomeEnum) that you then switch on
  • an array of a custom PairOfCharAndFunction class sorted on the character field that you search using binary search
  • a HashMap<Character, SomeFunction>
  • a TreeMap<Character, SomeFunction> or TreeMap<Character, SomeEnum> that you use to implement range-based checking: hint the floorEntry method.

And so on.


I know making an array would cost more memory but would it be better than using multiple (tons of ) if statement with respect to the performance ?

Yes it would, provided that the array (or similar mapping data structure; see above) only needs to be set up once.

It is theoretically possible for the Java JIT compiler to turn a sequence of if tests into the machine equivalent of a switch in some circumstances, but I don't think it does.

... would a switch statement be generally faster than if statements?

Generally yes. If not, the JIT compiler will translate the switch into a sequence of if statements. (In fact, there is a time versus code-space trade-off here.)

Upvotes: 1

Nicolas Filotto
Nicolas Filotto

Reputation: 45005

You should check range of characters instead of checking one character after the other. Something like

if (a < 'a' && a > 'z') {
    // do something
}

Upvotes: 1

Related Questions