Android: Can't decide on multiple if's or nested switch statements
I'm creating an android app which will call a method which parses the selected item of multiple spinner objects. I'm very new to Java, and I'm not sure which statement is best to use in terms of program flow in this instance. Should I just use multiple If statements? Such as:
If(spinner1.getSelectedItemPosition() == 0 && spinner2.getSelectedItemPosition() == 2 && spinner3.getSelectedItemPosition() == 4)
/* do some stuff
Or would it be better to use switch statements?
switch(spinner1.getSelectedItem())
case 1:
switch(spinner2.开发者_开发问答getSelectedItem())
case 1:
switch(spinner3.getSelectedItem())
case 1:
/* do something
Essentially what I'm trying to do here is evaluate the selected item of each spinner object, then create a new Activity based on that evaluation. However the initial activity consists of several spinner boxes, each with several options, so I'm not sure the best way to go about designing the control statements. Using an if statement for every single possible combination of selected items seems a bit unwieldy. Additionally, I'm not sure which option will actually make the code work the way I intend it to.
Choose readability above anything else, because in performance or program flow there are no big differences.
Probably a switch statement will be more effective, because you can send multiple results into the same section of code (sounds like that might be necessary for your application) like this
switch(value) {
case 0:
case 1:
//Do THIS
break;
case 2:
//Do THAT
break;
default:
//Doesn't match any
}
If value evaluates to either 0 or 1, the first section will be executed and you only had to write the operation code once. As for specifics (now I'm just getting creative), you could shift the three spinner values into a single integer and switch on the result. If you write the case statements in hexadecimal, it will be more readable as to the state of each.
int result = 0;
result += (byte)(spinner1.getSelectedItemPosition() << 16);
result += (byte)(spinner2.getSelectedItemPosition() << 8);
result += (byte)(spinner3.getSelectedItemPosition());
switch(result) {
case 0x000000: //All spinners 0
case 0x010201: //S1 = 1, S2 = 2, S3 = 1
//Do something
break;
case 0x010101: //S1 = 1, S2 = 1, S3 = 1
//Do something else
break;
default:
//Everything else (good if most of your options go to the same Activity)
}
Hope that Helps!
Possible drawback to this example: Only 256 options allowed for each spinner ;)
I think that almost depends on how many choices the user has, if they are too many, the switch may be the best choice.
However, you cans also arrange them in categories, and each one in different modules, in something like a progression page, every time the user has selected some of one categories, show the next one and so on
So each of four spinners has x states? Or do you want to capture just one specific state and have some 'else' branch for the others?
In the end this will all become very lengthy, so that cascaded switch statements may be easier to read.
Depending on the input data you could also do something else:
If you have 4 spinners, then multiply the position of the first by 1000, of the 2nd by 100 , the third by 10 and the last by 1. Then add the numbers together and have fund with a few thousand case:
statements ):-)
From a purely performance standpoint, switch statements are faster. The initial expression passed into the switch is evaluated once and then checked against the case values. For an if statement the both the left and right hand side of the if check is evaluated each time. So using if statements would be a bit slower.
精彩评论