Final answer:
To remove all even elements from an ArrayList of Integers, you can iterate over the ArrayList and remove any elements that are divisible by 2.
Step-by-step explanation:
To remove all even elements from an ArrayList of Integers, you can iterate over the ArrayList and remove any elements that are divisible by 2. You can use the % operator to check for divisibility. Here's how you can implement the removeEvens method:
public static void removeEvens(ArrayList list) {
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
int num = iterator.next();
if (num % 2 == 0) {
iterator.remove();
}
}
for (int num : list) {
System.out.println(num);
}
}
In the main method, you can call the removeEvens method passing the ArrayList as an argument:
public static void main(String[] args) {
ArrayList list = new ArrayList();
// Add elements to the list
removeEvens(list);
}
This code will remove all even elements from the ArrayList and print the remaining items.