views:

176

answers:

5

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  A: 
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];
Draco Ater
the same error unfortunately
Really? I also thought this would solve the your problem.
monn
@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.
Incognito
A: 

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.

Incognito
what will be solution?
If you need it just for test, this can be for (int t = i; t < (nums - 1); t++) k[t]=k[t+1];
Incognito
A: 

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. ;-)

InsertNickHere
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).
sateesh
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?
InsertNickHere
+1  A: 

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.

GK
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
+2  A: 

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

polygenelubricants