過(guò)渡變量
需求:拼接字符串,在每個(gè)字符串中間加",",但最后不加。現(xiàn)實(shí):直觀的方法,如果能知道iterator的大小,當(dāng)判斷到是最后的元素時(shí),就可以不拼接","。
因?yàn)槭?span style="color: rgb(0, 0, 0);">Iterator,不能事先知道size,用下面的方法。
1 String comma = "";
2 while (iterator.hasNext()) {
3 strSql.append(comma);
4 strSql.append((String)iterator.next());
5 comma = ",";
6 }
2 while (iterator.hasNext()) {
3 strSql.append(comma);
4 strSql.append((String)iterator.next());
5 comma = ",";
6 }
技巧:相當(dāng)于加了個(gè)中間過(guò)渡變量。
缺點(diǎn):每次循環(huán)都進(jìn)行一次賦值操作。
posted on 2009-06-11 20:46 游雯 閱讀(315) 評(píng)論(0) 編輯 收藏 所屬分類: Java編程技巧