2
class ArrayApp{

    public static void main(final String[] args){
        long[] arr; // reference to array
        arr = new long[100]; // make array
        int nElems = 0; // number of items
        int j; // loop counter
        long searchKey; // key of item to search for
        // --------------------------------------------------------------
        arr[0] = 77; // insert 10 items
        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;
        nElems = 10; // now 10 items in array
        // --------------------------------------------------------------
        for(j = 0; j < nElems; j++){
            System.out.print(arr[j] + " ");
        }
        System.out.println("");
        // --------------------------------------------------------------
        searchKey = 66; // find item with key 66
        for(j = 0; j < nElems; j++){
            if(arr[j] == searchKey){
                break; // yes, exit before end
            }
        }
        if(j == nElems){
            System.out.println("Can’t find " + searchKey); // yes
        } else{
            System.out.println("Found " + searchKey); // no
        }
        // --------------------------------------------------------------
        searchKey = 55; // delete item with key 55
        for(j = 0; j < nElems; j++){
            if(arr[j] == searchKey){
                break;
            }
        }
        for(int k = j; k < nElems - 1; k++){
            arr[k] = arr[k + 1];
        }
        nElems--; // decrement size
        // --------------------------------------------------------------
        for(j = 0; j < nElems; j++){
            System.out.print(arr[j] + " ");
        }
        System.out.println("");
    } // end main()
} // end class ArrayApp
  • Why do we use j and nElems to search an array.
  • Why do we again assign j to k for deletion? Can't we delete it from j itself?
1
  • Yea gods. Some simple formatting would make that lot easier to swallow. Commented Nov 15, 2010 at 13:16

5 Answers 5

0

nElemens is used to speed up the search. In the example above the array has 100 fields. So you would need to search all the 100 fields. But since nElemen (the number of elements) is only 10, it is just necessary to search 10 elements instead of all 100.

But be careful: the algorithm above assumes that the array is filled in the correct order and there can't be any gaps between to fields that have a value.

Then the variable j is used as a loop variable to access different fields in the array.

E.g.

arr[5] accesses the 6. field of the array. arr[j] accesses the j. element in the array. For basic information about java loops:

http://leepoint.net/notes-java/flow/loops/loops.html

Sign up to request clarification or add additional context in comments.

Comments

0

This is the difficult (not to mention long) way of removing an element from an array. There are other easier ways, including:

Check these on the Java docs.

1 Comment

True, but I presume the point here was to demonstrate how it works. arraycopy and ArrayList.remove do essentially the same thing as the above, though I presume more efficiently.
0

Actually, it is not a good practice to "remove" elements from [] in Java. They are statically allocated, and [].length cannot be changed anyway. What do you want to do? Use dynamic arrays, lists etc. For example, use ArrayList.

Comments

0

Your idea is to implement a "resizable" array upon a java array (which has a fixed size). The first nElem elements in this fixed array are your logical array. Now you want to be able to set and remove elements to and from this logical array:

[00] [01] [02] [03] [04] [05] [06] [07] [08] [09] ... [99] // physical array
[00] [01] [02] [03] [04] [05] [06] [07] [08]               // logical array, nElem = 9
Insert '99' at (logical) position 04
[00] [01] [02] [03] [99] [04] [05] [06] [07] [08]          // nElem = 10
Delete value at (logical) position 03
[00] [01] [02] [99] [04] [05] [06] [07] [08]               // nElem = 9

The size of the java array is still 100 (it's fixed), the size of the logical array (nEleme) has to be adjusted after each insert and remove operation.

If you insert an element, you have to "shift" some elements "to the right" (and increment nElem). You'll need a for loop and a counter (j) for this. The same happens if you want to remove an element, you have to "shift" elements "to the left" and again you need a counter.

Why do we use j and nElems to search an array.

To search for an item in an (unsorted) collection (array, set, ..) you'll have to look at each element, maybe starting at index=0 up to index=(nElem-1). If the value at the current position matches your search criteria (example: is-equal-to), you can break the search. j stores this actual index, nElem the size of the (logical) array so (nElem-1) the index of the last element in this (logical) array)

Why do we again assign j to k for deletion? Can't we delete it from j itself?

Merely for readbility. This will work too, but is much harder to understand.

for(j = 0; j < nElems; j++) {
   if(arr[j] == searchKey) {
       break;
   }
}
for(;j < nElems-1; j++) {  // note the missing initialzing value for the loop
   arr[j] = arr[j+1];
}

Comments

0

If you have Apache's commons-lang libary, you could try

inputArray = ArrayUtils.remove(inputArray , indexOfElement);

the method returns a new array, created by removing the found element from original array.

Comments

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.