In: Computer Science
Re-do Problem 2, but now implement the following method: public static void deleteEvenNumbers() This method must delete all even numbers in the chain pointed to by aList, while preserving the original of the remaining numbers. For example, if aList is this list: 3, 9, 2, 10, 5, 6; the list becomes after calling the method as follows: 3, 9, 5.
useing netbeans
import java.util.*;
public class Main
{
public
static void deleteEvenNumbers(List<Integer>numbers)
{
// Iterator = An iterator over a collection/List, hasNext()=Returns true if iteration has more elements, next()=Returns next element in a collection/List
for
(Iterator<Integer> iterator = numbers.iterator();
iterator.hasNext();){
Integer number = iterator.next();
// Checks if
number is even or not
if (number % 2 == 0) {
//remove the number
if its even
iterator.remove();
}
}
}
public static void main(String[] args)
{
//Creating an empty List to test our method
List<Integer> numbers = new ArrayList<Integer>();
//Adding the elements inside the List
numbers.add(11);
numbers.add(45);
numbers.add(12);
numbers.add(32);
numbers.add(36);
//Printing the List before calling the method
deleteEvenNumbers(List<Integer>numbers)
System.out.println(numbers);
deleteEvenNumbers(numbers);
//Printing the List after calling the method
deleteEvenNumbers(List<Integer>numbers)
System.out.println(numbers);
}
}