【问题标题】:Understanding the logic of this remove method in Java理解Java中这个remove方法的逻辑
【发布时间】:2017-01-31 20:27:50
【问题描述】:

此方法的作用是从数组中删除值 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;
        }
    }
}

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

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

我不明白布尔值!完成,布尔值让我感到困惑。 为什么在这个检查点之后有done = true?然后再检查一次是否(完成)?以及为什么另一个 for 循环 for(int i = position + 1; i

我知道当我们想要访问一个特定的索引值时,我们会编写 variablenameOfArr 然后 [] 并将其放入框内。我很难把这一切放在一起。

谢谢

【问题讨论】:

  • 您确定此代码仅删除第一次出现的值吗?在我看来,如果该值出现不止一次,它将删除多个元素(在第一个元素之后,不一定是匹配的元素之后)。也许if(done) 块末尾缺少return; 语句?
  • if(arr[pos] = toRemove)小心:这不是代码所说的,这也是无效的(这是一个赋值,而不是比较)。
  • 我刚刚编辑了代码以更正缩进。之前很纠结。正确的缩进可以帮助你更好地理解代码。
  • 你测试过这个方法吗?没看过,只是看了一下,觉得不太对。如果找到该值,则会将其删除,但随后会将剩余值移动太多次。

标签: java arrays


【解决方案1】:

在这种情况下,布尔值 done 似乎控制了一个值是否已经被删除。它以错误的方式开始算法,因为尚未删除任何内容。

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

一旦一个值被删除,done 就会被设置为 true,这样以后的值就不会被删除。

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

我希望这会有所帮助,如果出现任何其他问题,请发表评论。

【讨论】:

    【解决方案2】:
            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
                   }
               }
            }
    

    【讨论】:

      【解决方案3】:

      布尔值确实没有必要,因为当if(!done &amp;&amp; arr[pos] == toRemove) 为真时,它总是true
      此外,当您删除一个元素时,继续外循环是没有意义的: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;
          }
       }
      

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-30
        • 2019-11-10
        • 1970-01-01
        相关资源
        最近更新 更多