-3

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;

  }

}
1
  • arrays in java are 0 starting indexed, if you make an array of length 10 i.e. data = new String[10] then you can only call 0-9 to get each value. calling data[10] will throw an index out of bounds Commented Jan 22, 2015 at 20:09

1 Answer 1

0

Setting the maximum to a new number will not magically grow the array. In Java, arrays are created with a given size and that size does not change.

If you get to a situation where you have to grow your array because you have to put more elements in it, you have to create a new array, with the new maximum, copy all the existing elements into it, and then assign this new array to the field data.


I also believe that your setGrowBy() is probably not doing what it should. If you meant for it to grow the array, then it should do what I said above. But if you meant to set the growby variable, you should do that instead of changing the maximum.


Final note: the convention in Java is that method and variable names always start with a lower case letter. Only type names should start with an upper case letter (and constants are all uppercase). So you should change the names of your methods.

Sign up to request clarification or add additional context in comments.

2 Comments

Right thanks for all the answers , but I have got another thing to know which is how the " int FindItem(String search) " would look like in a recursive way instead of the way I have it right now . Thanks
That is a separate question. But you had better search for questions that were asked before, like this one or your question will be closed as a duplicate. Anyway, please remember that binary search only works if the array is sorted!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.