Reputation:
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
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
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
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
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:
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.
final
variables there are, the harder it is to understand what's going on as their values changes over time00
in the array. The 0
prefix is for octal literals. That is, 010 == 8
.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.Upvotes: 3
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
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
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
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