-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;

  }

}
ekcs
  • 21
  • 3
  • 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 – chancea Jan 22 '15 at 20:09

1 Answers1

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.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
  • 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 – ekcs Jan 22 '15 at 20:35
  • That is a separate question. But you had better search for questions that were asked before, like [this one](http://stackoverflow.com/questions/19492402/sorting-and-binary-search-using-java) or your question will be closed as a duplicate. Anyway, please remember that binary search only works if the array is sorted! – RealSkeptic Jan 22 '15 at 20:41