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 elements bigger than N from ArrayList in Java

1 Answer

0 votes
import java.util.*;

public class MyClass {
    public static <T> void remove(ArrayList<T> al, int n) { 
        Iterator it = al.iterator(); 
        while (it.hasNext()) { 
            int val = (Integer)it.next(); 
            if (val > n) 
                it.remove(); 
        } 
    } 
    public static void main(String args[]) {
        ArrayList<Integer> al = new ArrayList<>(Arrays.asList(5, 3, 8, 5, 0, 1, 99, 7, 4, 2)); 
 
        System.out.println(al); 
         
        int N = 5; 
        
        remove(al, N);

        System.out.println(al); 
  
    }
}
    
    
    
    
/*
run:
    
[5, 3, 8, 5, 0, 1, 99, 7, 4, 2]
[5, 3, 5, 0, 1, 4, 2]

*/

 



answered Apr 20, 2020 by avibootz

Related questions

4 answers 228 views
3 answers 163 views
1 answer 132 views
1 answer 172 views
...