3
public static List<Integer> removeOddNumbers(ArrayList<Integer> list) {
        if (list.isEmpty()) { throw new Error(); }
        List<Integer> toRemove = new ArrayList<>();

        for (int i : list) {
            if (i % 2 != 0) { toRemove.add(i); }
        }
        list.removeAll(toRemove);
        return list;
}

I'm trying to remove all odd elements from an ArrayList and then returning that ArrayList.

I'm getting an error pointing to List Integer in this first line

Test:

ArrayList<Integer> arrayList = new ArrayList<Integer>();
Collections.addAll(arrayList, 3, 5, 6, 24, 7, 9, 1, 8, 28, 11);
ArrayList<Integer> result = removeOddNumbers(arrayList);
System.out.println(result);

Result:

[6, 24, 8, 28]
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
qwert123
  • 57
  • 2
  • 7
  • What is the error you're getting? – sarkasronie Mar 16 '18 at 10:30
  • 1
    You can use another list to store the element you want to keep or iterate backwards. – assylias Mar 16 '18 at 10:30
  • Can you please add the code where you call `removeOddNumbers`? – sarkasronie Mar 16 '18 at 10:36
  • 1
    [Questions seeking debugging help (*"why isn't this code working?"*) must include the desired behavior, a **specific problem or error** and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.](https://stackoverflow.com/help/on-topic) See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Turing85 Mar 16 '18 at 10:41

4 Answers4

3

You should go for Iterator to remove items from List. That would save the usage of a temporary list where you store odd elements.

public static List<Integer> removeOddNumbers(List<Integer> arrayList) {
    Iterator<Integer> itr = arrayList .iterator();
    while (itr.hasNext())
    {
        int x = (Integer)itr.next();
        if (x % 2 != 0) {
            itr.remove();
        }
    } 
    return arrayList;
}

OR

You can go for a lot easier code with Java8.

2

If you are using Java 8, you can just use Collection::removeIf :

list.removeIf(i -> i % 2 != 0);

The full method should look like this :

public static List<Integer> removeOddNumbers(List<Integer> list) {
    list.removeIf(i -> i % 2 != 0);
    return list;
}

Example

List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 6, 5, 4));
list.removeIf(i -> i % 2 != 0);
System.out.println(removeOddNumbers(list));

Outputs

[2, 6, 4]
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
1

lambda way :

public static List<Integer> filter(List<Integer> numberList) {
    return numberList.stream()
        .filter(number -> number % 2 != 0)
        .collect(Collectors.toList());
}

You should call this method with this :

List<Integer> list = Arrays.asList(3, 5, 6, 24, 7, 9, 1, 8, 28, 11);
List<Integer> result = removeOddNumbers(numbers);
System.out.println(result);

The problem is that the returning type of the method is List<Integer> but your code expect an ArrayList the solution is to simply use the generic class List or if you want use your code it should be like this :

ArrayList<Integer> arrayList = new ArrayList<Integer>();
Collections.addAll(arrayList, 3, 5, 6, 24, 7, 9, 1, 8, 28, 11);
List<Integer> result = removeOddNumbers(arrayList);
System.out.println(result);

N.B The method posted by @YCF_L edit the list that you pass by the parameter, my method instead create a new list leaving the original list untouched

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Emax
  • 1,343
  • 2
  • 19
  • 30
0
// you can use Iterator.remove()
List arr = new ArrayList();
        arr .add(10);
        arr .add(20);
        arr .add(30);
        arr .add(1);
        arr .add(2);

        // Remove odd elements
        // Iterator.remove()
        Iterator itr = arr .iterator();
        while (itr.hasNext())
        {
            int x = (Integer)itr.next();
            if (x % 2 != 0)
                itr.remove();
        } 
        System.out.println("Modified ArrayList : "                                     + arr);