我需要检查一小部分逻辑,如果有人能给我一些宝贵的意见,我将非常感激.
我有两种检查逻辑的方法,想知道哪种方法更有效.
第一种方式:
if(url.equalsIgnoreCase("1")){
url = "aaa";
}
else if(url.equalsIgnoreCase("2")){
url = "bbb";
}
else if(url.equalsIgnoreCase("3")){
url = "ccc";
}
else if(url.equalsIgnoreCase("4")){
url = "ddd";
}
else if(url.equalsIgnoreCase("5")){
url = "eee";
}
else if(url.equalsIgnoreCase("6")){
url = "fff";
}
Run Code Online (Sandbox Code Playgroud)
第二种方式:
int temp = Integer.parseInt(url);
switch (temp) {
case 1:
url = "aaa";
break;
case 2:
url = "bbb";
break;
case 3:
url = "ccc";
break;
case 4:
url = "ddd";
break;
case 5:
url = "eee";
break;
case 6:
url = "fff";
break;
}
Run Code Online (Sandbox Code Playgroud)
请让我知道哪个更有效率.使用不Integer.parseInt(string)好吗?
njz*_*zk2 28
如果您的值确实是1-6,那么最清晰,最有效的方法是使用数组:
String[] URLS = {...};
url = URLS[Integer.parseInt(url) - 1];
Run Code Online (Sandbox Code Playgroud)
Phi*_*der 14
请让我知道哪个更有效率.
一个switch语句更有效率就是你的情况
使用Integer.parseInt(string)是不是很糟糕?
不,还好.但是当你使用java7时,你可以在你的交换机案例中使用String-constants值,但不能在Android上使用.
从效率来看:在大多数情况下,开关看起来更干净.
| 归档时间: |
|
| 查看次数: |
29957 次 |
| 最近记录: |