java Array : How to deal with an array condition in java

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?

Answers


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


Try using switch function.

for(int i=0; i

switch(i) { case 1: break;

//so on. }

}


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;

        }

u have to use switch case because ..

1:- Cleaner code. Code with multiple chained if/else if ... looks messy and is 
difficult to maintain - switch gives    cleaner structure.

condition is evaluated once is flexible enough to build things like the Duff's device as well as it executes much faster (via jump/lookup table) * historically

Ex:-

 switch (i){  
     case 1:
       // do something
       break;
     case 2:
       // do something
       break;
     case 3:
       // do something
       break;

    // and more...

     default:
       // do something 
   }

Need Your Help

The method setOnPageChangeListener(ViewPager.SimpleOnPageChangeListener) is undefined for the type Home.ImagePagerAdapter

java android android-viewpager fragmentpageradapter android-pageradapter

This is my first experience building something with ViewPager and I need a bit of help. I'm attempting to implement a solution found here:

Error messages returned from Web API method are omitted in non-dev environment

asp.net iis asp.net-web-api

I have a Web API controller POST method that behaves fine locally and on the testing server. If everything goes well it returns: