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

查看:119
本文介绍了用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--;
        }
    }

我理解第一个 循环和 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.

为什么的另一个循环?为什么我们开始第二个 -loop一个在最后一个之后?
为什么在第二个 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.

谢谢

推荐答案


为什么 -loop的另一个?为什么我们开始第二个 -loop一个在最后一个之后?为什么在第二个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 开始(因此 -loop从 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)而不是调用设置。这实际上删除了最后一项,并且没有留下魔数默认值。

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更新到左侧,即减去一个。 (实际上,你需要做的是在相同的索引开始迭代,但是因为 -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天全站免登陆