为什么这个循环只适用于一次迭代?

时间:2022-10-02 06:50:18

I have a loop that works to remove all the zeroes in the inputted array. I used the array:

我有一个循环,可以删除输入数组中的所有零。我使用了数组:

[[0, 0, 2, 2, 3, 4],[[0, 0, 2, 2, 3, 4]]

and this is the loop used to remove "all zeroes".

这是用于删除“全零”的循环。

public static int[][] removeTrivialCases(int[][]array)
    int[][] correctedArray = array;

    for (int i = 0; i < correctedArray.length; i++) {
        for (int j = 0; j < correctedArray[i].length; j++) {
            if (correctedArray[i][j] == 0) {
                correctedArray[i] = ArrayUtils.removeElement(correctedArray[i], j);
            }
        }
    }
return correctedArray;

When the above array is inputted, I only get this:

当输入上面的数组时,我只得到这个:

[[0, 2, 2, 3, 4],[0, 2, 2, 3, 4]]

Why does this loop only remove one set of zeroes from the arrays?

为什么这个循环只从数组中删除一组零?

Note: ArrayUtils is a class in the ApacheLang library.

注意:ArrayUtils是ApacheLang库中的一个类。

2 个解决方案

#1


2  

You have to be careful when you use an integer index to go through an array, at the same time you're removing elements from the array. Say your array is [0, 0, 2, 2, 3, 4]. You have a for loop like for (i = 0; i < a.length(); i++). When i is 0, you look up a.get(0), which is 0, and then you delete the element. This means the array is now [0, 2, 2, 3, 4]. Now the for loop increments i to 1. What happens when you look up a.get(1)? Shifting the elements in the array has caused your index to miss an element.

使用整数索引遍历数组时,必须小心,同时从数组中删除元素。假设你的数组是[0,0,2,2,3,4]。你有一个类似的for循环(i = 0; i ();>

There are at least a couple solutions to this common problem. One is to set up a new array to hold the results, so you're not shifting the elements in the array you're looping through. (For large arrays, that will be more efficient anyway, since you're not shifting the same elements multiple times--you will have an O(n) algorithm instead of O(n2).) Another is to set things up so that you increment the index only when you don't remove the element, something like

这个常见问题至少有几种解决方案。一种是设置一个新数组来保存结果,因此你不会移动你循环的数组中的元素。 (对于大型数组,无论如何都会更高效,因为你没有多次移动相同的元素 - 你将有一个O(n)算法而不是O(n2)。)另一个是设置,以便只有在不删除元素时才会增加索引

int i = 0;
while (i < a.length()) {
    look at a.get(i);
    ... see if you want to remove the element
    if (you want to remove the element) {
        call method to delete element from array;
    } else {
        i++;
    }
}

Now, since you increment i only when you don't delete the element, you will not skip any elements.

现在,因为只有在不删除元素时才增加i,所以不会跳过任何元素。

#2


0  

your problem is that when you increase j in each iteration you will skip element after removing one element because the array will shift there elements in each remove operation and you need to be sure about position of each element when removing it so you should adjust j with right index after each delete of element in your code [0, 0, 2, 2, 3, 4] when you remove element [0][0] from array the size of array will be 5 and first index of j will shifted to left [0, 2, 2, 3, 4] so when increase j you will skip the second 0 element in array

你的问题是,当你在每次迭代中增加j时,你会在删除一个元素后跳过元素,因为数组会在每个删除操作中移动元素,你需要确保每个元素在移除时的位置,所以你应该调整j在代码[0,0,2,2,3,4]中每次删除元素后右键索引从数组中删除元素[0] [0]时,数组的大小将为5,j的第一个索引将转移到左[0,2,2,3,4]所以当增加j时,你将跳过数组中的第二个0元素

you can correct your code in this way after each removing of element decrease j index by one :

每次删除元素减少j索引后,你可以用这种方式纠正你的代码:

 for (int i = 0; i < correctedArray.length; i++) {
        for (int j = 0; j < correctedArray[i].length; j++) {
            if (correctedArray[i][j] == 0) {
                correctedArray[i] = ArrayUtils.removeElement(correctedArray[i], j);
                j--;
            }
        }
     }

now when you remove element you won't skip next element when you increase j index

现在当你删除元素时,你不会在增加j索引时跳过下一个元素

I hope now the idea is clear!

我希望现在这个想法很明确!

#1


2  

You have to be careful when you use an integer index to go through an array, at the same time you're removing elements from the array. Say your array is [0, 0, 2, 2, 3, 4]. You have a for loop like for (i = 0; i < a.length(); i++). When i is 0, you look up a.get(0), which is 0, and then you delete the element. This means the array is now [0, 2, 2, 3, 4]. Now the for loop increments i to 1. What happens when you look up a.get(1)? Shifting the elements in the array has caused your index to miss an element.

使用整数索引遍历数组时,必须小心,同时从数组中删除元素。假设你的数组是[0,0,2,2,3,4]。你有一个类似的for循环(i = 0; i ();>

There are at least a couple solutions to this common problem. One is to set up a new array to hold the results, so you're not shifting the elements in the array you're looping through. (For large arrays, that will be more efficient anyway, since you're not shifting the same elements multiple times--you will have an O(n) algorithm instead of O(n2).) Another is to set things up so that you increment the index only when you don't remove the element, something like

这个常见问题至少有几种解决方案。一种是设置一个新数组来保存结果,因此你不会移动你循环的数组中的元素。 (对于大型数组,无论如何都会更高效,因为你没有多次移动相同的元素 - 你将有一个O(n)算法而不是O(n2)。)另一个是设置,以便只有在不删除元素时才会增加索引

int i = 0;
while (i < a.length()) {
    look at a.get(i);
    ... see if you want to remove the element
    if (you want to remove the element) {
        call method to delete element from array;
    } else {
        i++;
    }
}

Now, since you increment i only when you don't delete the element, you will not skip any elements.

现在,因为只有在不删除元素时才增加i,所以不会跳过任何元素。

#2


0  

your problem is that when you increase j in each iteration you will skip element after removing one element because the array will shift there elements in each remove operation and you need to be sure about position of each element when removing it so you should adjust j with right index after each delete of element in your code [0, 0, 2, 2, 3, 4] when you remove element [0][0] from array the size of array will be 5 and first index of j will shifted to left [0, 2, 2, 3, 4] so when increase j you will skip the second 0 element in array

你的问题是,当你在每次迭代中增加j时,你会在删除一个元素后跳过元素,因为数组会在每个删除操作中移动元素,你需要确保每个元素在移除时的位置,所以你应该调整j在代码[0,0,2,2,3,4]中每次删除元素后右键索引从数组中删除元素[0] [0]时,数组的大小将为5,j的第一个索引将转移到左[0,2,2,3,4]所以当增加j时,你将跳过数组中的第二个0元素

you can correct your code in this way after each removing of element decrease j index by one :

每次删除元素减少j索引后,你可以用这种方式纠正你的代码:

 for (int i = 0; i < correctedArray.length; i++) {
        for (int j = 0; j < correctedArray[i].length; j++) {
            if (correctedArray[i][j] == 0) {
                correctedArray[i] = ArrayUtils.removeElement(correctedArray[i], j);
                j--;
            }
        }
     }

now when you remove element you won't skip next element when you increase j index

现在当你删除元素时,你不会在增加j索引时跳过下一个元素

I hope now the idea is clear!

我希望现在这个想法很明确!