user466534
user466534

Reputation:

Delete item from array in Java

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

Upvotes: 1

Views: 10020

Answers (8)

user1693371
user1693371

Reputation: 124

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

Upvotes: 0

RicNjesh
RicNjesh

Reputation: 583

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

Upvotes: 0

pabitra
pabitra

Reputation: 1

    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.

Upvotes: 0

polygenelubricants
polygenelubricants

Reputation: 384016

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

Upvotes: 3

Draco Ater
Draco Ater

Reputation: 21226

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

Upvotes: 5

GuruKulki
GuruKulki

Reputation: 26428

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.

Upvotes: 1

InsertNickHere
InsertNickHere

Reputation: 3666

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

Upvotes: 0

Incognito
Incognito

Reputation: 16597

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.

Upvotes: 0

Related Questions