0

i'm learning about array algorithm sorting and with one example i'm quite confused about the following array deletion code in Java. Specifically the portion where higher element values are being moved left on the array order (starting at the line (for int k=j, k < nElems -1, k++...) at the very bottom, and the two lines below that). Would appreciate some direction as to what is happening there please? Thanks much!

public class ArrayApp {
    public static void main(String args[]) {
        int nElems = 10;
        int[] arr = new int[nElems];
        int j;
        int searchKey;
        arr[0] = 77;
        arr[1] = 99;
        arr[2] = 44;
        arr[3] = 55;
        arr[4] = 22;
        arr[5] = 88;
        arr[6] = 11;
        arr[7] = 00;
        arr[8] = 66;
        arr[9] = 33;
        for (j = 0; j < nElems; j++) {
            System.out.print(arr[j] + " ");
        }
        System.out.println();
        //Find 66 in array
        searchKey = 66;
        for (j = 0; j < nElems; j++) {
            if (arr[j] == searchKey) {
                break;
            }
        }
        if (j == nElems) {
            System.out.println("Cant find " + searchKey);
        } else {
            System.out.println("Found " + searchKey + " in position " + j);
        }
        //Remove 55 from array
        searchKey = 55; // delete item with key 55
        for (j = 0; j < nElems; j++) { // look for it
            if (arr[j] == searchKey) {
                break;
            }
        }
        for (int k = j; k < nElems - 1; k++) { // move higher ones down
            arr[k] = arr[k + 1];
        }
        nElems--;
        for (j = 0; j < nElems; j++) {
            System.out.print(arr[j] + " ");
        }
    }
}
2
  • This is a typical [teach-me] question. Finding someone you could sit down with and use a piece of paper to explain what's happening is much easier than answering this with a text. Commented Jan 26, 2018 at 18:51
  • This algorithm is actually inefficient. A swap would be faster, instead of shifting all array elements (however, a shift is necessary if the array is ordered in alphabetical order, for example). Commented Jan 26, 2018 at 18:55

2 Answers 2

1

You cannot "delete" an element form an array. An array has a fixed length. You can only shift elements to replace it.

Original array: 0 1 2 3 4 5 A B C D E F

Let's "delete" D from it:

0 1 2 3 4 5
A B C D E F
A B C  <------ Leave them as is
      E F <--- Shift these to the left
          Z <- Fill the vacant last element with something.

The result is

0 1 2 3 4 5
A B C E F Z

I hope this helps.

This loop does the shifting. Here j is the index of the element being deleted (of the D above; it would be 3). Starting from it, elements move one index value to the left.

for (int k = j; // starting from the index of the element we trample.
     k < nElems - 1; // up to the last element of the array.
     k++ // incrementing k at every iteration
) { 
   // set element value (arr[k]) to the value to the right to it (arr[k+1])
   arr[k] = arr[k + 1];  
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you 9000. But could you please explain what's happening in the k=j for loop?
I up voted your answer as well 9000 but same issue. Thanks
ELI5'd the answer.
0

Once the element 55 is found in the array you know the index of that element. In order to remove this element you need to swap the value with a different value. Thus, the loop, where k = j, shifts all elements forward one to remove the value. The downside of doing this is the last element in the array is not removed from the array. However, nElems is decreased so the loops with not reach this value in future calls.

4 Comments

Thanks csell! That cleared it up for me! I realize now that k=j means k=3 since the J++ loop above stops at arr[3]. Much appreciated.
@powershellFan83 You're welcome! Remember to mark an answer as correct for any future users with the same question.
I up voted your answer but it says my score is too low for it to be recorded publicly but will record it "internally". Thanks again csell!
@powershellFan83 Thanks! If you click the checkmark next to one of the answers it will make that answer the selected solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.