使用 arrayList 理解这个 removeAll java 方法 [英] Understanding this removeAll java method with arrayList

查看:23
本文介绍了使用 arrayList 理解这个 removeAll java 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此方法的职责是从arrayList 中删除所有出现的值toRemove.剩下的元素应该只是移向列表的开头(大小不会改变).最后的所有额外"元素(尽管列表中出现了多次 toRemove)应该只填充 0.该方法没有返回值,如果列表没有元素,它应该只是没有效果.不能使用 ArrayList 类中的 remove()removeAll().

This method's duty is to remove all occurrences of the value toRemove from the arrayList. The remaining elements should just be shifted toward the beginning of the list (the size will not change). All "extra" elements at the end (however many occurrences of toRemove were in the list) should just be filled with 0. The method has no return value, and if the list has no elements it should just have no effect. Cannot use remove() and removeAll() from the ArrayList class.

方法签名为:

public static void removeAll(ArrayList<Integer> list, int toRemove);

解决办法:

public static void removeAll(ArrayList<Integer> list, int toRemove) {
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i) = toRemove) {
            for (int j = i + 1; j < list.size(); j++) {
                list.set(j - 1, list.get(j));
            }
            list.set(list.size() - 1, 0);
            i--;
        }
    }

我理解第一个 for 循环和 if 语句.因为人们想要一个一个地遍历整个 arrayList,并且对于每个索引,并且在 arrayList 中存在一个数字,检查它是否是,实际上是 toRemovee 整数.在这一点之后,我迷失了.

I understand the first for loop and the if statement well. Because one would want to iterate through the entire arrayList one-by-one and for each index with a number present in the arrayList check if it is, in fact the toRemovee integer. After this point I become lost.

为什么还有一个 for 循环?为什么我们在最后一个循环之后开始第二个 for 循环?为什么在第二个 for 循环中我们使用 list.set(),我知道 set 方法接受两个参数,索引位置和要子到该指定位置的元素.为什么是 j-1?为什么在第二个循环结束后还有一行: list.set(list.size()-1, 0) ?为什么是 i--?

Why another for loop? Why do we start the second for-loop one after where the last one was? Why within that second for loop do we use list.set(), I understand that set method takes in two arguments, the index position and the element to sub into that designated position. Why j-1? Why after this second loop is over is there the line: list.set(list.size()-1, 0) ? Why the i--?

有很多活动部分,我想了解其中的逻辑.

There are a lot of moving parts and I would like to understand the logic.

谢谢

推荐答案

为什么还有一个 for 循环?为什么我们在最后一个循环之后开始第二个 for 循环?为什么在第二个 for 循环中我们使用 list.set(),我知道 set 方法接受两个参数,索引位置和要子到该指定位置的元素.为什么 j - 1?

Why another for-loop? Why do we start the second for-loop one after where the last one was? Why within that second for loop do we use list.set(), I understand that set method takes in two arguments, the index position, and the element to sub into that designated position. Why j - 1?

该方法完成了文档所说的一切.内循环将元素左移一个.

The method does everything the documentation says. The inner loop shifts the elements left by one.

for (int j = i + 1; j < list.size(); j++) {
    list.set(j - 1,       //The index before j (that is, j - 1) is set to...
            list.get(j)); //the current element.
    }
}

从找到的索引之后的元素开始,将左边的那个设置为当前元素.最终,您会发现所有元素都向左移动.

Starting at the element after the found index, it sets the one to the left to the current element. Eventually, you'll end up with all elements having been shifted left.

list.set(list.size() - 1, 0);

这会将最后一个元素设置为零.由于您的列表删除了一个元素,因此您需要删除最后一个元素,因为所有内容都已移动.

This sets the last element to zero. Since your list has had one element removed, you need to get rid of the last one because everything has been shifted.

示例:

在这些例子中,^ 是 i 而 *j.

In these examples, ^ is i and * is j.

0 1 2 3 4

说我要删除 2.
首先,我会循环直到找到 2.

Say I want to remove 2.
First, I'll loop until I find 2.

0 1 2 3 4
    ^ index: 2

现在,我将移动剩下的所有内容以删除该元素.因为我们要左移,所以需要从 i + 1 开始(因此 for 循环从 i + 1 开始.>

Now, I'll shift everything left to remove that element. Because we're shifting left, we need to start at i + 1 (hence the for-loop starting at i + 1.

0 1 3 3 4
    ^ * (replaced the one before *)

接下来,我们再做一次.

Next, we do it again.

0 1 3 4 4
    ^   *

现在我们完成了.

list.set(list.size() - 1, 0);

list.set(list.size() - 1, 0);

然而,最后还剩 4 个.我们需要删除它,因为我们删除了一个元素,并且列表短了一个元素.因此,我们将最后一个元素设置为零以将其删除(当然,这假定零不是列表中的有效值).

However, 4 is left at the end. We need to remove that because we removed an element, and the list is one element shorter. So, we set the last element to zero to remove it (of course, this assumes zero is not a valid value in the list).

注意:

我强烈建议使用 list.remove(list.size() - 1) 而不是调用 set.这实际上删除了最后一个项目,并没有为其保留幻数"默认值.

I would strongly suggest doing list.remove(list.size() - 1) instead of the call to set. This actually removes the last item and does not leave it with a "magic number" default value.

为什么是 i--?

您需要 i-- 因为所有内容都已向左移动,因此您需要向左更新索引 1,即减去 1.(实际上,您需要做的是在相同索引处开始迭代,但是由于for-loop 会在每次迭代时执行i++i-- 来取消"它.)

You need i-- because everything has been shifted left, so you'll need to update the index 1 to the left, namely by subtracting one. (Actually, what you need to do is start the iteration at the same index, but since the for-loop executes i++ every iteration you need to do i-- to "cancel" it out.)

这篇关于使用 arrayList 理解这个 removeAll java 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆