1

What is wrong here? I want delete an item from an array, but it shows me

error ArrayIndexOutBound exception

public class delete {

    public static void main(String[]args) {
        int i;

        //delete  item from array
        int k[] = new int[]{77,99,44,11,00,55,66,33,10};

        //delete 55
        int searchkey=55;

        int nums=k.length;
        for ( i=0;i<nums;i++)
            if (k[i]==searchkey)
                break;

        for (int t=i;t<nums;t++)
            k[t]=k[t+1];
        nums--;

        for (int m=0;m<nums;m++) {
            System.out.println(k[m]);
        }
    }
}
3
  • Please format this question so it's legible and add a language tag. Commented May 14, 2010 at 7:13
  • Note that arrays in Java have a fixed length, you cannot really delete an element from an array (so that the array becomes shorter by one element). Commented May 14, 2010 at 8:04
  • If you do not want to preserve the order of elements, just replace the element in question with the last element and set the last element to null. Commented May 15, 2010 at 21:19

8 Answers 8

5
for (int t=i;t<nums-1;t++)  //Should be -1 here, as k[t+1] will be out of bounds if t = nums-1

Or another variant to nums-- before you move the numbers

nums--;
for (int t=i;t<nums;t++)
   k[t]=k[t+1];
Sign up to request clarification or add additional context in comments.

1 Comment

@davit-datuashvili: This should work, but just not add nums-- before loop. You need to replace the nums-- after loop with the one before it and it will work.
3

The following rewriting should be instructive:

public class Delete {
    static int search(int key, int[] arr) {
        for (int i = 0; i < arr.length; i++)
            if (arr[i] == key) {
                return i;
            }
        return -1;
    }
    static void print(int[] arr, final int L) {
        for (int i = 0; i < L; i++) {
            System.out.println(arr[i]);
            // try this also:
            // System.out.format("%02d ", arr[i]);          
        }
    }
    public static void main(String[] args) {
        int nums[] = { 77, 99, 44, 11, 00, 55, 66, 33, 10 };
        final int N = nums.length;
        int searchKey = 55;

        int pos = search(searchKey, nums);
        for (int t = pos; t < N-1; t++) {
            nums[t] = nums[t + 1];
        }
        print(nums, N-1);
        // prints 77, 99, 44, 11, 0, 66, 33, 10
        System.out.println(010 == 8); // prints "true"
        System.out.println(00000); // prints "0
    }
}

Here are some key observations:

  • Break apart logic into helper methods. This makes the logical components easier to test and reuse, and the overall logic easier to understand.
  • It makes the code easier to understand if you use final local variables like N to denote the initial size of int[] nums, and define the rest of the logic in terms of N, N-1, etc.
    • The more non-final variables there are, the harder it is to understand what's going on as their values changes over time
  • Follow coding convention. In particular, class names starts with uppercase.
  • Do be careful with the 00 in the array. The 0 prefix is for octal literals. That is, 010 == 8.
  • Do note that 00 is printed as simple 0. Numerically, 00 = 000 = 0000 = 0. If you need this to be zero-padded, then that's a formatting issue.

See also

On octal literals

On zero-padding

Comments

1

in the following loop

for (int t=i;t<nums;t++)
   k[t]=k[t+1];

when t is pointing to the last element then k[t+1] operation will throw an exception which is what you are getting now.

1 Comment

thanks everybody i have tried once and didnot work maybe because of i have not make executable file properly i am writing programs in linux text editior and maybe because of this something about compile was wrong
0

On k[t]=k[t+1]; you got error es k[t+1] tries to access 10th element with index 9, but your array contains 9 elements. So you got data out of bounds.

1 Comment

If you need it just for test, this can be for (int t = i; t < (nums - 1); t++) k[t]=k[t+1];
0

It works if you use it as Draco Ater said:

for (int t=i;t<nums-1;t++) {
    k[t]=k[t+1];
}
nums--;

Output is then: 77 99 44 11 0 66 33 10

which should be correct. ;-)

2 Comments

Draco's suggestion was to decrement "nums" before moving around array elements, what you have posted here is "nums" after the swap (which already the OP is doing).
The first line of dracos answer: for (int t=i;t<nums-1;t++) The other with nums-- befor the loop is "another variant to". So why downvote?
0
    for (int t=i;t<nums;t++)
      k[t]=k[t+1];

just replace nums with nums-1 because you have already deleted(skipped) one element.

Comments

0

I find it to work best in the following way:

Make sure the iteration does not go beyond the second last element (array.length-1) so it can have an element to compare to:

for(int i=elementPosition-1;i<array.length-1;i++){array[i]=array[i+1];}

Comments

0
import java.util.ArrayList;
import java.util.Arrays;

public class Sort {
    public static void main(String a[]) {
        int swap;
        int length;
        int[] unsorted = { 1, 2, 4, 3, 6, 5, 7, 8, 18, 17, 65, 46, 2, 4, 5, 3,
                4 };
        length = unsorted.length;
        for (int i = 0; i < length; i++) {
            for (int j = i + 1; j < length; j++) {
                if (unsorted[i] > unsorted[j]) {
                    swap = unsorted[i];
                    unsorted[i] = unsorted[j];
                    unsorted[j] = swap;
                } else if (unsorted[i] == unsorted[j]) {
                    for (int k = j; k < length - 1; k++) {
                        unsorted[k] = unsorted[k + 1];
                    }
                    length -= 1;
                }
            }
        }
        for (int i = 0; i < length; i++) {
            System.out.println(" " + i + "th element " + unsorted[i]);
        }
    }
}

Comments

Your Answer

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