android如何从if-else切换到切换案例

时间:2015-03-11 08:33:50

标签: android

我有关于pmt功能的应用程序。但是,有很多条件需要处理。不知何故,该应用程序无法使用超过12 if-else。我想使用switch case,但我还是不太懂得如何使用switch case(自从我第一次尝试使用eclipse以来已经过了1个半月)。任何一个例子都将受到高度赞赏。

这是我的示例代码:

    if(String1.toString().equals("condition1")){
          //do something
          if(String2.toString().equals("condition1.1")&& String3.toString().equals("condition1.2")){
        //do something else
    }
    .
    .
    .
    .
    .
      if(String2.toString().equals("condition1.##")&& String3.toString().equals("condition1.##")){
        //do something else
    }
    }
else if(String1.toString().equals("condition2")){
          //do something
          if(String2.toString().equals("condition2.1")&& String3.toString().equals("condition2.2")){
        //do something else
    }
    .
    .
    .
    .
    .
      if(String2.toString().equals("condition2.##")&& String3.toString().equals("condition2.##")){
        //do something else
    }
    }
if(String1.toString().equals("condition3")){
          //do something
          if(String2.toString().equals("condition3.1")&& String3.toString().equals("condition3.2")){
        //do something else
    }
    .
    .
    .
    .
    .
      if(String2.toString().equals("condition3.##")&& String3.toString().equals("condition3.##")){
        //do something else
    }
    }

仍然继续......处理所有可能性。我想知道,如何在开关案例中这样做。如果我们有3次3条件,或者更好的实施。例如a,b,c(假设这三个条件只能使用一次)和d,e,f和g,h,i则条件1是a,d,g;条件2是a,d,h条件3是a,d,i;条件4 a,e,g ........等等

注意:假设API版本是8-11(旧的android)

感谢

2 个答案:

答案 0 :(得分:1)

因为支持java 1.7开启字符串。

你可以宣布两个开关:

switch(String1) {
  case "condition1": {
      switch(String2) {
         case "condition1.1":
              break;
           // ... other cases
          default:
              break;
      }
  }
  break;
  // ... other cases
  default break;
}

答案 1 :(得分:1)

答案取决于你的目标版本的android。从KitKat及以上(API Level 19+),Java 7的switch (String)可用。我还强烈建议尝试将子类(条件n.x)分组为不同的方法。它的速度非常快,否则:

switch (String1.toString) {
case "condition1":
    handleCase1(String2, String3);
    break;

case "condition2":
     handleCase2(String2, String3);
     break;
}

如果仍然导致代码过于复杂,您可以尝试查找表和命令模式

class ConditionKey {
    final String String1;
    final String String2;
    final String String3;

    public int hashCode(); // hash strings
    public boolean equals(); // compare strings
}

interface ConditionCommand {
   // use whatever arguments the operation needs, you can also
   // add fields and initialize in the constructor
   void perform(final ConditionKey key, /* [...] */);
}

Map<ConditionKey, ConditionCommand> actionMap = new HashMap<>();
actionMap.put(
  new ConditionKey("condition1", "condition1.1", "condition1.2"),
  new ConditionCommand() {
      void perform(final ConditionKey key) {
         // perform  actions that need to be done
      }
  }
);

然后代替if-elseswitch-case

[...]
ConditionKey key = new ConditionKey(string1, string2, string3);
// get the action from the map
ConditionCommand command = actionMap.get(key);
// perform the command
command.perform(key);