码迷,mamicode.com
首页 > Web开发 > 详细

http://wsfly.iteye.com/blog/1152248

时间:2016-12-12 23:58:55      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:int   contains   new   static   sys   font   ring   com   []   

public class Demo {
    /**
     * 去掉重复值
     */
    public static void main(String[] args) {
        String test = "100,120,166,1555,120,150,100";
        String[] test1 = test.split(",");
        ArrayList list = new ArrayList();
        for (int i = 0; i < test1.length; i++) {
            if (!list.contains(test1[i]))
                list.add(test1[i]);
        }
        for (int i = 0; i < list.size(); i++)
            System.out.println(list.get(i));
    }
}

这个方法是很好,但是还有更加简单的

public class Demo {
    /**
     * 去掉重复值
     */
    public static void main(String[] args) {
        String test = "100,120,166,1555,120,150,100";
        String[] test1 = test.split(",");
        List list = Arrays.asList(test1);
        Set set = new HashSet(list);
        String [] rid=(String [])set.toArray(new String[0]);
        for (int i = 0; i < rid.length; i++) {
            System.out.println(rid[i]);
        }
    }
}

 

http://wsfly.iteye.com/blog/1152248

标签:int   contains   new   static   sys   font   ring   com   []   

原文地址:http://www.cnblogs.com/winner-0715/p/6166025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!