奇怪的 For 循环行为
我在包含 if-else if-else 块的 for 循环中看到奇怪的行为。
如果我执行以下操作,它会按预期工作,但列表中不存在的名称除外:
Resources res = context.getResources();
String[] debtnameslist = res.getStringArray(R.array.debtnames);
for(int i=1; i < debtnameslist.length; i++){
if (debtname.toLowerCase().contains(debtnameslist[i]))
{
int resID = context.getResources().getIdentifier("ic_dialog_"+debtnameslist[i], "drawable", "com.freshsoft.android.debtdestroyer");
icon.setImageResource(resID);
}
else if ((debtname.toLowerCase().contains("best") && (debtname.toLowerCase().contains("buy"))))
{
icon.setImageResource(R.drawable.ic_dialog_bestbuy);
}
// else {
// icon.setImageResource(R.drawable.ic_dialog_dollar);
// }
}
给我这个: http://i54.tinypic.com/rsr33q.png
它可以工作,除了“测试” " 应该显示一个通用符号。如果我取消注释 //else 部分,则会得到以下内容:
http://i54.tinypic.com /25kj6z6.png
我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="debtnames">
<item >chase</item>
<item >discover</item>
<item >citibank</item>
<item >amazon</item>
<item >american express</item>
<item >diners</item>
<item >mastercard</item>
<item >visa</item>
<item >paypal</item>
<item >macys</item>
<item >bmw</item>
<item >target</item>
</string-array>
</resources>
I'm seeing strange behavior in a for loop that contains an if-else if-else block.
If I do the following, it works as expected, except for the names that don't exist in my list:
Resources res = context.getResources();
String[] debtnameslist = res.getStringArray(R.array.debtnames);
for(int i=1; i < debtnameslist.length; i++){
if (debtname.toLowerCase().contains(debtnameslist[i]))
{
int resID = context.getResources().getIdentifier("ic_dialog_"+debtnameslist[i], "drawable", "com.freshsoft.android.debtdestroyer");
icon.setImageResource(resID);
}
else if ((debtname.toLowerCase().contains("best") && (debtname.toLowerCase().contains("buy"))))
{
icon.setImageResource(R.drawable.ic_dialog_bestbuy);
}
// else {
// icon.setImageResource(R.drawable.ic_dialog_dollar);
// }
}
gives me this:
http://i54.tinypic.com/rsr33q.png
it works, except for the "test" that should show a generic symbol. If I un-comment the //else portion, I get the following:
http://i54.tinypic.com/25kj6z6.png
my xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="debtnames">
<item >chase</item>
<item >discover</item>
<item >citibank</item>
<item >amazon</item>
<item >american express</item>
<item >diners</item>
<item >mastercard</item>
<item >visa</item>
<item >paypal</item>
<item >macys</item>
<item >bmw</item>
<item >target</item>
</string-array>
</resources>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是 - 如果
debtnameslist[i]
不包含debtnames
中的任何名称,也不包含“best buy”,它会放置美元图标 - 因为第一个if
为 false,第二个也为 false。您不能在其中放置非条件
else
子句,因为它将覆盖下一个循环中的图标!正如您所看到的,在这种情况下唯一有效的是“目标” - 因为它是最后一个,所以没有下一个循环来覆盖其图标。
The problem is - if
debtnameslist[i]
does not contain any of the names indebtnames
nor "best buy", It will put the dollar icon - because the firstif
is false, the second is false too.You can't put a non conditional
else
clauses in there, because it will override the icon in the next loop!And as you can see, the only 1 that works in this case is "target" - because it is last, theres no next loop to override its icon.
经过一段时间的折腾后弄清楚了。诀窍是如果找到匹配则打破循环:
Figured it out after screwing around with it for a while. The trick was to break the loop if a match was found: