0
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?

1
  • Obtain the length of the array directly, Bag.length, rather than duplicating that information in another variable. Commented Apr 13, 2012 at 12:13

3 Answers 3

3

Ok I would also advocate using some sort of switch function, and would structure my code as follows

public void fooOperation(int[] numbers) {
    for(int i=0; i < numbers.length; i++) { 
        handleValue(i);
    }
}

public void handleValue(int value)  {
    switch(i) {
        case 1:
            // some code
            break;
        case 2:
            // some code
           break;
        default:
            break;
    }
}

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

Sign up to request clarification or add additional context in comments.

Comments

1

Try using switch function.

for(int i=0; i

switch(i) { case 1: break;

//so on. }

}

Comments

0

Switch case is best for this requirement.when you morethan 3 conditions to check ,you can go for switch.

   switch (i) {
            case 0:
                System.out.println("i value"+i);

                break;
            case 1:
                System.out.println("i value"+i);

                break;
            case 2:
                System.out.println("i value"+i);

                break;
            case 3:
                System.out.println("i value"+i);

                break;
            case 4:
                System.out.println("i value"+i);

                break;

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.