简体   繁体   English

理解Java中这个remove方法的逻辑

[英]Understanding the logic of this remove method in Java

The job of this method is to remove the value toRemove from the array. 此方法的作用是从数组中删除值toRemove。 The remaining elements should just be shifted toward the beginning of the array. 剩下的元素应该只是移向数组的开头。 (The array's size will not change.) Since the array will now have one fewer element, the position of the last element should just be filled with 0. If there is more than one occurrence of toRemove in the array, only the first occurrence should be removed. (数组的大小不会改变。)由于数组现在只有少一个元素,最后一个元素的位置应该只用0填充。如果数组中出现多次toRemove,则只有第一次出现被删除。 The method has no return value, and if the array has no elements, it should just have no effect. 该方法没有返回值,如果数组没有元素,它应该没有效果。

The solution: 解决方案:

public static void remove(int[] arr, int toRemove) {
    boolean done = false;
    int position = 0;
    for(int pos = 0; pos < arr.length; pos++) {
        if(!done && arr[pos] == toRemove) {
            done = true;
            position = pos;
        }
        if(done) {
            for(int i = position + 1; i < arr.length; i++) {
                arr[i - 1] = arr[i];
            }
            arr[arr.length -1] = 0;
        }
    }
}

I am not following how this algorithm works. 我没有关注这个算法是如何工作的。 The use of boolean confuses me, I feel I don't fully understand what the primitive data type does, I know that it holds two things either true or false, and false by default. 布尔的使用让我感到困惑,我觉得我并不完全理解原始数据类型的作用,我知道它有两个是true或false,默认是false。 But what does that really mean? 但这究竟意味着什么? I don't understand boolean. 我不明白布尔。 I understand why would want an int placeholder for the index of where the toRemove value was found. 我理解为什么需要一个int占位符来找到找到toRemove值的索引。 I understand we would want to use a for-loop to iterate one-by-one the indices and their respective values and pinpoint where exactly toRemove is found. 据我所知,我们希望使用for循环逐个迭代索引及其各自的值,并精确定位找到toRemove的位置。 I understand we would want a check point conditional to see if at some arbitrary index the toRemove value exists, andd therefore: 我知道我们需要一个检查点条件,以查看在某个任意索引处是否存在toRemove值,因此:

if(arr[pos] = toRemove) // then bingo we've found him

I don't understand the boolean !done, booleans confuse me. 我不明白布尔!完成,布尔人迷惑我。 why after this check point is there done = true? 为什么在这个检查点之后是完成=真? and then after that another check if(done)? 然后再检查一下(完成)? and why another for loop for(int i = position + 1; i < arr.length; i++) and after that for loop the line arr[i - 1] = arr[i];? 为什么另一个for循环为(int i = position + 1; i <arr.length; i ++),之后为循环行arr [i-1] = arr [i];? and finally at the end arr[arr.length-1] = 0 and position = pos; 最后是arr [arr.length-1] = 0和position = pos;

I understand when we want to access a particular indicies value we write variablenameOfArr then the [] and put it inside the box. 我知道当我们想要访问特定的指标值时,我们写下variablenameOfArr然后将[]放在框中。 I am having difficulty putting this all together. 我很难把这一切都放在一起。

Thank you 谢谢

        public static void remove(int[] arr, int toRemove) {
            boolean done = false; //This boolean is used to determine when the element has been found
            int position = 0;
              for(int pos = 0; pos < arr.length; pos++) { //Iterating through the array
    //if we aren't already done, (!done = NOT DONE) and we have found the position to remove, then enter this logic
                if(!done && arr[pos] == toRemove) { 
                  done = true; //since we found the position to remove, set done to true
                  position = pos; //Save the index of the one that was removed
                }
                if(done) { //if we are done, enter this logic
//This loop starts above the index where removed, and iterates to the top
                  for(int i = position + 1; i < arr.length; i++) {
                    arr[i - 1] = arr[i]; //This shifts each element down one
                }
                arr[arr.length -1] = 0; //This sets the empty slot at the top of the array to 0
               }
           }
        }

In this case, the boolean done seems to control whether a value has already been removed or not. 在这种情况下,boolean done似乎控制是否已经删除了一个值。 It begins the algorithm as false, as nothing has been removed yet. 它开始算法为假,因为还没有删除任何内容。

The first if statement tests to see if the value of done is false. 第一个if语句测试以查看done的值是否为false。 In if statements, instead of saying if(done == false), this can be simplified to if(!done). 在if语句中,而不是说if(done == false),这可以简化为if(!done)。 So, this if statement simply tests to see if a value has been found yet or not. 因此,这个if语句只是测试是否已经找到一个值。

done is then set to true once a value has been removed, so that no future values will be removed. 一旦删除了值,则将done设置为true,以便不会删除将来的值。

Finally, the second if statement tests to see if a value has been removed or not. 最后,第二个if语句测试是否已删除值。 Like the first if statement, if(done == true) can be simplified to if(done). 与第一个if语句一样,if(done == true)可以简化为if(done)。

I hope this helps, comment with any further questions if they arise. 我希望这有帮助,如果出现任何进一步的问题,请评论。

The boolean is indeed not necessary since it is always true when if(!done && arr[pos] == toRemove) is true. 布尔确实是没有必要的,因为它始终是trueif(!done && arr[pos] == toRemove)是真实的。
Besides, it makes no sense to go on the outer loop when you have removed an element as 1) the state of the array is nice : the inner loop has shifted the elements after the removed element to their left and 2) you cannot perform two removals. 除此之外,当你删除一个元素时继续外循环是没有意义的1)数组的状态很好:内部循环已经移除了元素后面的元素到左边2)你不能执行两个清除。

By the way the position variable is also not required. 顺便说一下, position变量也不是必需的。 You can use the pos variable directly as it is read only used. 您可以直接使用pos变量,因为它是只读的。

This code : 这段代码:

  for(int pos = 0; pos < arr.length; pos++) {

    if(!done && arr[pos] == toRemove) {
      done = true;
      position = pos;
    }
    if(done) {
      for(int i = position + 1; i < arr.length; i++) {
        arr[i - 1] = arr[i];
      }
      arr[arr.length -1] = 0;
    }
 }

could be replaced by this without using the boolean and you could also exist the method after the array elements were shifted : 可以在不使用布尔值的情况下替换它,并且在数组元素移位后您也可以存在该方法:

  for(int pos = 0; pos < arr.length; pos++) {

    if(arr[pos] == toRemove) {  
      for(int i = pos + 1; i < arr.length; i++) {
        arr[i - 1] = arr[i];
      }
      arr[arr.length -1] = 0;        
      return;
    }

  }

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

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