0

Hello I have got this basically fully working sorted vector , the problem here is however that I can only initialize the array to a fixed size before inserting any values , so for example I can initialize 5 but if I want to insert 6 items it gives me a null pointer exception . I think I do understand what is happening however I would like anybody to show me a solution how the array size can be increased automatically every time I want to insert something . ( Without having to use any inbuilt java functionalities like ArrayList )

Thank you


package ads2;

public class SortedVector2
{
    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;  


    String[] data;

    public SortedVector2() 
    {

        length = 0;

        maximum = 5;
        data = new String[maximum];


    }

    public void AddItem(String value) 
    {

        /*if (length == maximum)    
        {
        maximum += 200000;
        */

        data[length] = value;

        length++;
      //  SetSorted();
       // SetSorted(data);

    }

    public void SetSorted() 
    {

        for (int j = 0; j < data.length - 1; j++) {
           if (data[j].compareTo(data[j + 1]) > -1) {
                String temp = data[j];
                data[j] = data[j + 1];
                data[j + 1] = temp;
            }

       }
        for (String s : data) {
         System.out.println(s);
       }

       // private String[] data;

        /*
       for(int i = data.length-1; i >= 0; i--) {
       for(int j = 0; j < i; j++) {
        if(data[j].compareTo(data[j + 1]) > -1) {
            String temp = data[j];
            data[j] = data[j + 1];
            data[j + 1] = temp;
        }
       }
      }  for (String s : data) {
        System.out.println(s);
    }
                */
    }

    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 InsertItem(int index, String value) 
    {
        if (length == maximum) 
        {

        maximum += 200000;

        }

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

}
5
  • 1
    Why don't you want to use ArrayList? Commented Feb 9, 2015 at 12:33
  • 3
    You cannot change the size of an array at runtime. You can however make a new one and copy everything to it. Commented Feb 9, 2015 at 12:33
  • Not possible. either use ArrayList or you have to reinitialize the list yourself if you need more size. Commented Feb 9, 2015 at 12:33
  • Hi ThePerson , what do you mean by " make a new one and copy everything to it " ? Commented Feb 9, 2015 at 12:37
  • use Arrays.copyOf. An answer to it you can see it 2dArray increase size. Hope this helps. Commented Feb 9, 2015 at 12:41

5 Answers 5

1

You have to do what the implementers of ArrayList did. When you try to add an element when the array is full, you create a larger array, copy the existing elements to it and add the new element.

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

Comments

1

To increase array size dynamically use Collection framework interface List, It has implementation ArrayList,Vector and LinkedList use any one in them.

Or, Simply create copyArray(String[]) api which will give you array with increased capacity.

public String[] copyArray(String[] oldArray){
  int capacity = oldArray.length * 2;
  return Arrays.copyOf(oldArray, capacity);
}

String[] data = copyArray(data) // pass array 

Comments

0

I think you've got all the basic variables you need to do what you need to do: just check if the size equals the capacity when you are adding an item and if it does reallocate the array:

if (size == capacity) {
    capacity += growby;
    data = Arrays.copyOf(data, capacity);
}

That's pretty much all ArrayList does.

Comments

0

You need to re-allocate when increasing the size of the data buffer, for example,

public void InsertItem(int index, String value) 
{
  String[] data2;
  if (length == (maximum-1))
  {
    maximum += 5; // increment size in lot of 5
    data2 = new String[maximum);
    for (int ii = 0; ii < length; ii++) 
    {
      data2[ii] = date[ii];
    }
    data = data2; // re-assign with increased size
  }
  for(int i = length - 1; i >= index; --i) 
  {
    data[i + 1] = data[i];
  }
  data[index] = value;
  length++;
}

Comments

0

In software engineering there is a saying, "Don't reinvent the wheel" - which emphasizes us on using the existing archetype. Because they are tested and used by for long period of time. So it's better to use ArrayList for regular/professional purpose.

But it if it is for learning purpose then you can chose any one from the previous answers.

Comments

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.