# how to delete an element from a sorted array

• 07-12-2012, 10:02 PM
gnawk
how to delete an element from a sorted array
Hi im trying to delete a value from a sorted array but it does not seem to work
when i run the program and try to delete a value it only decrements the size of the array by one.
So the last value gets deleted everytime

Code:

```        public void delete(Comparable T) {                 int f = find(T);                 if (empty() == true) {                         System.out.println("The array is empty and nothing to delete.");                         return;                 }                 for (int i = f; i == top - 1; i++){                         array[i + 1] = array[i];                 }                 top--;         } public int find(Comparable T) {                 int start = 0;                 int end = top;                 int midPoint;                 while (start <= end) {                         midPoint = (start + end) / 2;                         if (array[midPoint].compareTo(T) == 0) {                                 return midPoint;                         } else if (array[midPoint].compareTo(T) < 0) {                                 start = midPoint + 1;                         } else                                 end = midPoint - 1;                 }                 return -1;         }```
• 07-12-2012, 10:22 PM
JosAH
Re: how to delete an element from a sorted array
Quote:

Originally Posted by gnawk
Code:

```                for (int i = f; i == top - 1; i++){                         array[i + 1] = array[i];                 }                 top--;         }```

I don't think this entire loop makes any sense ...

kind regards,

Jos
• 07-12-2012, 10:48 PM
gnawk
Re: how to delete an element from a sorted array
Fixed it.... It was
For (i =f; i<= top; i++)
Not i==top