Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to remove multiple elements from ArrayList in Java

3 Answers

0 votes
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
  
public class MyClass {
    public static void main(String args[]) {
        List<Integer> al = new ArrayList<Integer>();
        al.add(12);
        al.add(90);
        al.add(87);
        al.add(42);
        al.add(100);
        al.add(51);
        al.add(99);
        al.add(121);
 
        System.out.println(al);
 
        Iterator<Integer> itr = al.iterator();
        while (itr.hasNext()) {
            Integer number = itr.next();
 
            if (number % 2 == 0) {
                itr.remove();
            }
        }
 
        System.out.println(al);
    }
}
       
       
       
       
       
/*
run:
       
[12, 90, 87, 42, 100, 51, 99, 121]
[87, 51, 99, 121]
   
*/

 



answered Oct 17, 2023 by avibootz
0 votes
import java.util.ArrayList;
import java.util.List;

public class MyClass {
    public static void main(String args[]) {
        List<Integer> al = new ArrayList<Integer>();
        al.add(12);
        al.add(90);
        al.add(87);
        al.add(32);
        al.add(100);
        al.add(51);
        al.add(99);
        al.add(121);
 
        System.out.println(al);
 
        al.subList(1, 4).clear();

        System.out.println(al);
    }
}
       
       
       
       
       
/*
run:
       
[12, 90, 87, 32, 100, 51, 99, 121]
[12, 100, 51, 99, 121]
   
*/

 



answered Oct 17, 2023 by avibootz
0 votes
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyClass {
    public static void main(String args[]) {
        List<Integer> al = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
        List<Integer> itemstoremove = new ArrayList<>(Arrays.asList(2, 3, 8));
        
        al.removeAll(itemstoremove);
        
        System.out.println(al);
     }
}
       
       
       
       
       
/*
run:
       
[0, 1, 4, 5, 6, 7, 9]
   
*/

 



answered Oct 17, 2023 by avibootz

Related questions

1 answer 132 views
1 answer 131 views
1 answer 172 views
2 answers 139 views
1 answer 72 views
...