int Cnt = 4;
for(int i=0; i<Cnt; i++) {
}
Inside this loop I want to make different decisions based on the value of i.
So what is the best way to implmenet this ??
Should i use the following way ??
for(int i=0; i<Cnt; i++) {
if (i==0) {
// some code
}
else if(i==1) {
// some code
}
}
Or will this way it will look good ??
for (int i=0; i<Cnt; i++) {
String str = Bag[0].value ;
String str22 = Bag[1].value
}
I want to ask is that how can I know what the maximum length of array can be?
What is the least error prone way for doing this?
Ok I would also advocate using some sort of switch function, and would structure my code as follows
Something else worthy of note is if this switch statement is going to get very big its not the best solution you could use the strategy pattern to avoid such code. It does however depend on what is held in your array, the ints may represent something else (the value of an enum value for example) at which point you can convert to a strongly typed case and have a behavior for that case. This question is a matter of software engineering and really depends on the scope of what is received. If you are only ever going to have 3 cases there’s an argument that says simple a switch is best because it is just that, Simple.
The reason I have not done the switch inline is to make that method easier to unit test without entering a loop meaning each case can be tested in isolation and when you change it you do not have to worry about accidently modifying the loop code… its tidier