How to remove element from ArrayList by checking its value?

后端 未结 11 1205
一整个雨季
一整个雨季 2020-12-01 03:47

I have ArrayList, from which I want to remove an element which has particular value...

for eg.

ArrayList a=new ArrayList         


        
11条回答
  •  一向
    一向 (楼主)
    2020-12-01 03:58

    This will give you the output,

        ArrayList l= new ArrayList();
    
        String[] str={"16","b","c","d","e","16","f","g","16","b"};
        ArrayList tempList= new ArrayList();
    
        for(String s:str){
            l.add(s);
        }
    
        ArrayList duplicates= new ArrayList();
    
        for (String dupWord : l) {
            if (!tempList.contains(dupWord)) {
                tempList.add(dupWord);
            }else{
                duplicates.add(dupWord);
            }
        }
    
        for(String check : duplicates){
            if(tempList.contains(check)){
                tempList.remove(check);
            }
        }
    
        System.out.println(tempList);
    

    output,

    [c, d, e, f, g]
    

提交回复
热议问题