聂永的博客

记录工作/学习的点点滴滴。

如何删除List数组里面特定元素

/**
* 学习List数组删除特定元素
*/
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();
    // 可以简单删除数据
    Iterator<String> iter = list.iterator();
    while (iter.hasNext()) {
        t = iter.next();
        if (t.endsWith("2") || t.endsWith("4") || t.endsWith("6")) {
            iter.remove();
        }
    }

    // 会造成数据删除未达到效果
    /*
     * 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); } }
     */
    // 会造成删除引发异常
    /*
     * 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接口才得以实现。

posted on 2009-05-04 15:44 nieyong 阅读(2733) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

公告

所有文章皆为原创,若转载请标明出处,谢谢~

新浪微博,欢迎关注:

导航

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

统计

常用链接

留言簿(58)

随笔分类(130)

随笔档案(151)

个人收藏

最新随笔

搜索

最新评论

阅读排行榜

评论排行榜