简体   繁体   中英

Understanding the logic of this remove method in Java

The job of this method is to remove the value toRemove from the array. 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. 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. 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. 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. I understand we would want a check point conditional to see if at some arbitrary index the toRemove value exists, andd therefore:

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];? and finally at the end arr[arr.length-1] = 0 and position = pos;

I understand when we want to access a particular indicies value we write variablenameOfArr then the [] and put it inside the box. 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. 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. In if statements, instead of saying if(done == false), this can be simplified to if(!done). So, this if statement simply tests to see if a value has been found yet or not.

done is then set to true once a value has been removed, so that no future values will be removed.

Finally, the second if statement tests to see if a value has been removed or not. Like the first if statement, if(done == true) can be simplified to 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.
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.

By the way the position variable is also not required. You can use the pos variable directly as it is read only used.

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

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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