聶永的博客

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

          如何刪除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();
              // 可以簡單刪除數(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();
                  }
              }

              // 會造成數(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); } }
               */
              // 會造成刪除引發(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("使用時間 :" + (e - s));

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

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

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


          只有注冊用戶登錄后才能發(fā)表評論。


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

          公告

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

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

          導(dǎo)航

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

          統(tǒng)計

          常用鏈接

          留言簿(58)

          隨筆分類(130)

          隨筆檔案(151)

          個人收藏

          最新隨筆

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 麻阳| 化德县| 大洼县| 兴业县| 琼结县| 正宁县| 深泽县| 蒙城县| 韶关市| 沧源| 葫芦岛市| 横峰县| 新建县| 鄂托克前旗| 南华县| 郴州市| 永修县| 西青区| 新宾| 乐山市| 乐安县| 高阳县| 宜宾市| 青田县| 镇沅| 宁德市| 喀什市| 武汉市| 宜兰县| 灵川县| 濉溪县| 梧州市| 铁岭市| 重庆市| 高碑店市| 修武县| 呈贡县| 鲁甸县| 阿瓦提县| 开鲁县| 郎溪县|