I have got this code in java for a sorted vector , but I have a problem , somehow it is not adding correctly , you can get errors like " Exception: java.lang.ArrayIndexOutOfBoundsException: 10 " . Perhaps a just a slight modification is needed , but I am not seeing it . Can anybody please help me to make this work ?
Thank you
package ads2;
public class SortedVector {
private int length;
private int maximum;
private int growby;
private int temp;
private int x = 0;
private int high;
private int middle;
private int low;
private String[] data;
public SortedVector()
{
length = 0;
maximum = 10;
data = new String[maximum];
}
public void SetSorted() {
}
public void SetGrowBy(int growby)
{
maximum += growby;
}
public int GetCapacity()
{
return maximum;
}
public int GetNoOfItems()
{
return length;
}
public String GetItemByIndex(int index)
{
return data[index];
}
public int FindItem(String search)
{
for (x=0;x<=length; )
{
middle =((low + high)/2);
if (data[middle].compareTo(search)==0)
{
return middle;
}
else if (data[middle].compareTo(search)<0)
{
low = middle;
x++;
return FindItem(search);
}
else
{
high = middle;
x++;
return FindItem(search);
}
}
return -1;
}
public boolean Exists(String search)
{
boolean output;
int y;
y = 0;
while (data[y] != search && (length - 1) > y)
{
++y;
}
if (data[y] == search)
{
output = true;
} else
{
output = false;
}
y = 0;
return output;
}
public void AddItem(String value)
{
if (length == maximum)
{
maximum += 10;
}
data[length] = value;
length++;
}
public void InsertItem(int index, String value)
{
if (length == maximum)
{
maximum += 10;
}
for(int i = length - 1; i >= index; --i)
{
data[i + 1] = data[i];
}
data[index] = value;
length++;
}
public void DeleteItem(int index)
{
for(int x = index; x < length - 2; ++x)
{
data[x] = data[x + 1];
}
length--;
}
public String toString()
{
String res = "";
for (int i=0; i<length; i++)
res+=data[i] + "; ";
return res;
}
}