簡體   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