ekcs
ekcs

Reputation: 21

Automatic increment array java

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

}

Upvotes: 0

Views: 1758

Answers (5)

Razib
Razib

Reputation: 11163

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.

Upvotes: 0

atish shimpi
atish shimpi

Reputation: 5023

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 

Upvotes: 1

Dr. Debasish Jana
Dr. Debasish Jana

Reputation: 7118

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

Upvotes: 0

sprinter
sprinter

Reputation: 27956

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.

Upvotes: 0

Eran
Eran

Reputation: 393831

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.

Upvotes: 1

Related Questions