简体   繁体   English

从ArrayList中删除奇数元素

[英]Remove odd elements from ArrayList

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. 我试图从ArrayList中删除所有奇数元素,然后返回该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]

If you are using Java 8, you can just use Collection::removeIf : 如果您使用的是Java 8,则可以只使用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]

You should go for Iterator to remove items from List. 您应该使用Iterator从列表中删除项目。 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. 您可以使用Java8获得更简单的代码。

lambda way : lambda方法:

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 : 问题在于该方法的返回类型为List<Integer>但是您的代码期望ArrayList的解决方案是简单地使用通用类List或者如果您想使用代码,则它应该是这样的:

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);

NB 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 注意 @YCF_L发布的方法编辑您通过参数传递的列表,我的方法改为创建一个新列表,使原始列表保持不变

// 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM