我有兩個數組,一個是cityUSA [i],另一個是decimalUSA [i]。每個有超過1500個條目,cityUSA [100]以及decimalUSA [100]等等。我發現這個城市的人們正在使用Android的定位服務,然後將其與我在cityUSA [i]數組中的城市列表進行比較。然後我搜索一個匹配,並使用匹配的i來找到該數組中的decimalUSA [i]的相關值。這裏是代碼:在Java/Android中正確使用for和if語句
loc.getLatitude(); loc.getLongitude(); Geocoder geocoder = new Geocoder(rate.this,Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
TextView rateText = (TextView)findViewById(R.id.taxRate);
TextView locationText = (TextView)findViewById(R.id.taxLocation);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
String city = returnedAddress.getLocality();
locationText.setText(city);
int i;
for (i = 0; i <= cityUSA.length; i++){
if (cityUSA[i] == city) {
String PrecentString = decimalRate[i];
rateText.setText(PrecentString);
break;
}
}
}
else{
locationText.setText("No City returned!");
rateText.setText("No Rate returned!");
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
TextView locationText = (TextView)findViewById(R.id.Rate);
locationText.setText("Cannot get Location!");
}
應用程序的炸彈,當我嘗試運行它。如果我刪除以下表述:
int i;
for (i = 0; i <= cityUSA.length; i++){
if (cityUSA[i] == city) {
String PrecentString = decimalRate[i];
rateText.setText(PrecentString);
break;
}
}
它不會彈出,但它然後又不執行搜索。
有什麼建議嗎?
作爲附加評論,請儘量遵守Java命名約定。我看到你有一個名爲「PrecentString」的字符串(原文如此)。堅持命名約定會產生「percentString」。只是一個快速提示(當其他人閱讀你的代碼時很有用)。 – Vinay 2011-05-05 00:26:16