How do I remove repeated elements from ArrayList?

后端 未结 30 2260
难免孤独
难免孤独 2020-11-21 06:24

I have an ArrayList, and I want to remove repeated strings from it. How can I do this?

30条回答
  •  不要未来只要你来
    2020-11-21 06:47

    you can use nested loop in follow :

    ArrayList l1 = new ArrayList();
    ArrayList l2 = new ArrayList();
    
            Iterator iterator1 = l1.iterator();
            boolean repeated = false;
    
            while (iterator1.hasNext())
            {
                Class1 c1 = (Class1) iterator1.next();
                for (Class1 _c: l2) {
                    if(_c.getId() == c1.getId())
                        repeated = true;
                }
                if(!repeated)
                    l2.add(c1);
            }
    

提交回复
热议问题