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]);
}
}
}
The following rewriting should be instructive:
Here are some key observations:
finallocal variables likeNto denote the initial size ofint[] nums, and define the rest of the logic in terms ofN,N-1, etc.finalvariables there are, the harder it is to understand what’s going on as their values changes over time00in the array. The0prefix is for octal literals. That is,010 == 8.00is printed as simple0. 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