Switch case with multiple values

20,722

Solution 1

I think I wouldn't youse a switch in this case. I would do probably something like this

if(Arrays.asList(yourArray).contains(yourValue)){
   //do something
}else{
   //do something else
}

Solution 2

You can remove the break to get OR

switch(code){
 case case1:   
 case case2:
    doSomething();
  break;
}
Share:
20,722
dsk
Author by

dsk

Updated on July 02, 2020

Comments

  • dsk
    dsk almost 4 years

    Is there something like array of case statement and put it as single case statement into switch- suppose

    String[] statements={"height","HEIGHT"};
    

    and then

    switch(code){
     case statements: 
      //code here
      break;
     case something_else:
      break;
    }
    

    so if we add values into String array then it will automatically matched from that array in switch? like

      var1||var2||whatever //accessed from array or anything else for matching
    

    is there any implementation similar like this?