powershellFan83
powershellFan83

Reputation: 23

confused about array element deletion in Java

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] + " ");
        }
    }
}

Upvotes: 0

Views: 72

Answers (2)

9000
9000

Reputation: 40894

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

Upvotes: 1

sellc
sellc

Reputation: 378

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.

Upvotes: 0

Related Questions