事情是這樣的。我需要從一個ArrayList取出字串,將字串轉成Integer型別之後,以300為一個區間分別加入map裡面。數字範圍為0~3300,而我因為趕著要做出畫面給老闆看,也沒有去多加思考怎樣的寫法比較好,便很直覺的寫出以下程式:
for(int i = 0 ; i < list.size(); i++){
Exhibitor exhibitor = list.get(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("_id", exhibitor.getPK_ID().toString());
map.put("name", exhibitor.getName());
map.put("booth", exhibitor.getBoothId());
if(Integer.valueOf(exhibitor.getBoothId()) < 301){
map.put("separator", "1~300");
}else if(Integer.valueOf(exhibitor.getBoothId()) > 300 && Integer.valueOf(exhibitor.getBoothId()) < 601){
map.put("separator", "301~600");
}else if(Integer.valueOf(exhibitor.getBoothId()) > 600 && Integer.valueOf(exhibitor.getBoothId()) < 901){
map.put("separator", "601~900");
}else if(Integer.valueOf(exhibitor.getBoothId()) > 900 && Integer.valueOf(exhibitor.getBoothId()) < 1201){
map.put("separator", "901~1200");
}else if(Integer.valueOf(exhibitor.getBoothId()) > 1200 && Integer.valueOf(exhibitor.getBoothId()) < 1501){
map.put("separator", "1201~1500");
}else if(Integer.valueOf(exhibitor.getBoothId()) > 1500 && Integer.valueOf(exhibitor.getBoothId()) < 1801){
map.put("separator", "1501~1800");
}else if(Integer.valueOf(exhibitor.getBoothId()) > 1800 && Integer.valueOf(exhibitor.getBoothId()) < 2101){
map.put("separator", "1801~2100");
}else if(Integer.valueOf(exhibitor.getBoothId()) > 2100 && Integer.valueOf(exhibitor.getBoothId()) < 2401){
map.put("separator", "2101~2400");
}else if(Integer.valueOf(exhibitor.getBoothId()) > 2400 && Integer.valueOf(exhibitor.getBoothId()) < 2701){
map.put("separator", "2401~2700");
}else if(Integer.valueOf(exhibitor.getBoothId()) > 2700 && Integer.valueOf(exhibitor.getBoothId()) < 3001){
map.put("separator", "2701~3000");
}else if(Integer.valueOf(exhibitor.getBoothId()) > 3000 && Integer.valueOf(exhibitor.getBoothId()) < 3301){
map.put("separator", "3001~3300");
}
items.add(map);
}
這實在是太瞎了,身為一個軟體工程師,這樣的code給其他人看到也實在是太丟臉了,而且假設之後變為200個數字一組,且數字最大到10000,難道又要繼續寫n個if/else嗎?跑步跑了幾公里回來有了新想法,將以上程式改為:
for(int i = 0 ; i < list.size(); i++){
Exhibitor exhibitor = list.get(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("_id", exhibitor.getPK_ID().toString());
map.put("name", exhibitor.getName());
map.put("booth", exhibitor.getBoothId());
for(int x = 0 ; x<3001; x=x+300){
for(int y = 300; y< 3301; y= y+300){
int booth = Integer.valueOf(exhibitor.getBoothId());
if(booth > x && booth<y){//如果數字介於x與y之間
map.put("separator", String.valueOf(x+1)+"~"+String.valueOf(x+300));
}
}
}
items.add(map);
}
20行的程式碼縮短為8行,且維護也較容易。
不過這個雙重迴圈的最外層其實還有一個迴圈(因為數值從一個ArrayList中取出),從外到內讓程式跑了三個迴圈之後,執行變得非常的沒有效率,故又可以將以上程式碼改為以下:
int booth = Integer.valueOf(exhibitor.getBoothId());
double x = Math.floor(booth/300);//此方法為無條件捨去小數點,只取整數的部分
map.put("separator", String.valueOf(x*300+1)+"~"+String.valueOf((x+1)*300));
原本寫了將近20行的if/else判斷式,精簡成3行,結束。
不過因為運用了Math.floor得到的數值型別為double,故會有小數點,需再將其NumberFornat成整數再放置於map中:
for(int i = 0 ; i < list.size(); i++){
Exhibitor exhibitor = list.get(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("_id", exhibitor.getPK_ID().toString());
map.put("name", exhibitor.getName());
map.put("booth", exhibitor.getBoothId());
int booth = Integer.valueOf(exhibitor.getBoothId());
double x = Math.floor(booth/300);
NumberFormat formatter = new DecimalFormat("##");
double tempX = x*300+1;
double tempY = (x+1)*300;
String limitX = formatter.format(tempX);
String limitY = formatter.format(tempY);
map.put("separator",limitX+"~"+limitY);
items.add(map);
}
以上,收工。
0 comments:
Post a Comment