Answers for "remove some values"

0

remove some values

ArrayList -- Remove some values
Given a list of Integers 1, 2, 3, 4, 5, 6 ....etc. 
remove all values greater than 100.
  
Solution 1:
ArrayList<Integer> list1 =
  new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,101,200,300));
ArrayList<Integer> list2 = new ArrayList<>();
    for(int  each : lists1) 
    if( each < 100)
    lists2.add(each);
    list1=list2;     
    System.out.println(list1);
 
Solution 2:
List<Integer> list = 
  new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,101,200,300));
        Iterator<Integer> it = list.iterator();
        while(it.hasNext())
            if(it.next()>100)
                  it.remove();
         
        System.out.println(list);
Posted by: Guest on September-29-2021

Code answers related to "remove some values"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language