How to remove element from ArrayList by checking its value?

后端 未结 11 1214
一整个雨季
一整个雨季 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:59

    Try below code :

     public static void main(String[] args) throws Exception{
         List l = new ArrayList();
         l.add("abc");
         l.add("xyz");
         l.add("test");
         l.add("test123");
         System.out.println(l);
         List dl = new ArrayList();
        for (int i = 0; i < l.size(); i++) {
             String a = l.get(i);
             System.out.println(a); 
             if(a.equals("test")){
                 dl.add(a);
             }
        }
        l.removeAll(dl);
         System.out.println(l); 
    }
    

    your output :

     [abc, xyz, test, test123]
    abc
    xyz
    test
    test123
    [abc, xyz, test123]
    

提交回复
热议问题