聶永的博客

          記錄工作/學(xué)習(xí)的點(diǎn)點(diǎn)滴滴。

          如何刪除List數(shù)組里面特定元素

          /**
          * 學(xué)習(xí)List數(shù)組刪除特定元素
          */
          public static void main(String[] args) {

              List<String> list = new LinkedList<String>();

              for (int i = 0; i < 10; i++) {
                  list.add(Float.toString(RandomUtils.nextFloat()) + Long.valueOf(RandomUtils.nextLong()));
              }

              for (String s : list) {
                  System.out.println(s);
              }

              String t = null;

              long s = System.currentTimeMillis();
              // 可以簡(jiǎn)單刪除數(shù)據(jù)
              Iterator<String> iter = list.iterator();
              while (iter.hasNext()) {
                  t = iter.next();
                  if (t.endsWith("2") || t.endsWith("4") || t.endsWith("6")) {
                      iter.remove();
                  }
              }

              // 會(huì)造成數(shù)據(jù)刪除未達(dá)到效果
              /*
               * for(int i = 0; i < list.size(); i ++){ t = list.get(i);
               * if(t.endsWith("2") || t.endsWith("4") || t.endsWith("6")){
               * list.remove(i); } }
               */
              // 會(huì)造成刪除引發(fā)異常
              /*
               * for(String t1 : list){ t = t1; if(t.endsWith("2") || t.endsWith("4") ||
               * t.endsWith("6")){ list.remove(t1); } }
               */

              long e = System.currentTimeMillis();

              System.out.println("使用時(shí)間 :" + (e - s));

              for (String v : list) {
                  System.out.println(v);
              }
          }

                 上面代碼使用到了interator接口才得以實(shí)現(xiàn)。

          posted on 2009-05-04 15:44 nieyong 閱讀(2744) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          公告

          所有文章皆為原創(chuàng),若轉(zhuǎn)載請(qǐng)標(biāo)明出處,謝謝~

          新浪微博,歡迎關(guān)注:

          導(dǎo)航

          <2009年5月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統(tǒng)計(jì)

          常用鏈接

          留言簿(58)

          隨筆分類(lèi)(130)

          隨筆檔案(151)

          個(gè)人收藏

          最新隨筆

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 娱乐| 西乡县| 建昌县| 嘉祥县| 萝北县| 永胜县| 长治市| 昌乐县| 英山县| 临颍县| 蚌埠市| 武安市| 根河市| 阳泉市| 崇文区| 洛扎县| 舞钢市| 通河县| 成都市| 黄冈市| 云林县| 双流县| 襄垣县| 曲靖市| 土默特右旗| 青冈县| 凭祥市| 库伦旗| 庆元县| 浦县| 布尔津县| 星子县| 雅安市| 文山县| 高陵县| 江口县| 宜良县| 苍梧县| 云安县| 遂平县| 龙海市|