I was wondering if somebody could give me some pointers for a piece of work that I'm currently working on.
The concept is that I've got to create a Sorted Vector, using a String Array list, and be able to Add, Delete and Find items from the array.
I'm currently really struggling with the Adding of an Item.
In the constructor we have:
private int maxlength;
private int numberofitems;
private String[] data;
private int growby;
And they've been initalized with the values:
maxlength = 10;
numberofitems = 0;
data=new String[maxlength];
growby=10;
I then have a function that takes a String Value and needs to insert the value into the array:
public void AddItem(String value)
{
if (maxlength == data.length)
{
GrowArray();
}
//Need Help here
}
^^ That is the point at which I need help.
GrowArray simply creates a temp Array which increases the maxlength by 10, and copies across all the values from the old array to the new one.
private void GrowArray()
{
String[] data2=new String[data.length+growby];
System.arraycopy(data, 0, data2, 0, data.length);
data=data2;
}
My logic is this:
- I need to loop through the array and compare the search Value being against each item in the array and see whether it belongs before or after the current array value.
I know this should look something like: data[i].compareTo(value) <0;
Any and all help would be greatly appreciated. I should also mention that I'm not allowed to use collections.
Thanks!