繁体   English   中英

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

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

此方法的作用是从数组中删除值toRemove。 剩下的元素应该只是移向数组的开头。 (数组的大小不会改变。)由于数组现在只有少一个元素,最后一个元素的位置应该只用0填充。如果数组中出现多次toRemove,则只有第一次出现被删除。 该方法没有返回值,如果数组没有元素,它应该没有效果。

解决方案:

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

我没有关注这个算法是如何工作的。 布尔的使用让我感到困惑,我觉得我并不完全理解原始数据类型的作用,我知道它有两个是true或false,默认是false。 但这究竟意味着什么? 我不明白布尔。 我理解为什么需要一个int占位符来找到找到toRemove值的索引。 据我所知,我们希望使用for循环逐个迭代索引及其各自的值,并精确定位找到toRemove的位置。 我知道我们需要一个检查点条件,以查看在某个任意索引处是否存在toRemove值,因此:

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

我不明白布尔!完成,布尔人迷惑我。 为什么在这个检查点之后是完成=真? 然后再检查一下(完成)? 为什么另一个for循环为(int i = position + 1; i <arr.length; i ++),之后为循环行arr [i-1] = arr [i];? 最后是arr [arr.length-1] = 0和position = pos;

我知道当我们想要访问特定的指标值时,我们写下variablenameOfArr然后将[]放在框中。 我很难把这一切都放在一起。

谢谢

        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
               }
           }
        }

在这种情况下,boolean done似乎控制是否已经删除了一个值。 它开始算法为假,因为还没有删除任何内容。

第一个if语句测试以查看done的值是否为false。 在if语句中,而不是说if(done == false),这可以简化为if(!done)。 因此,这个if语句只是测试是否已经找到一个值。

一旦删除了值,则将done设置为true,以便不会删除将来的值。

最后,第二个if语句测试是否已删除值。 与第一个if语句一样,if(done == true)可以简化为if(done)。

我希望这有帮助,如果出现任何进一步的问题,请评论。

布尔确实是没有必要的,因为它始终是trueif(!done && arr[pos] == toRemove)是真实的。
除此之外,当你删除一个元素时继续外循环是没有意义的1)数组的状态很好:内部循环已经移除了元素后面的元素到左边2)你不能执行两个清除。

顺便说一下, position变量也不是必需的。 您可以直接使用pos变量,因为它是只读的。

这段代码:

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

可以在不使用布尔值的情况下替换它,并且在数组元素移位后您也可以存在该方法:

  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