0

I am trying to write my own version of a C++ STL vector<> I want it to make a dynamically growing and shrinking list...I want to have methods for push_back, push_front, pop_back, pop_front, remove, removeAt, clear, and size...I wrote all of these with little problem and am getting no build errors or warnings, when I try to instantiate the class Eclipse tells me that its looking for Dimensions after token...so it thinks any type I send in wants to be an array of that type, for instance...

    DynaArray<int> Bob;

Here it wants the [] operator after the int.

Here is what my class presently looks like

    public class DynaArray<T>
{       
    int Size = 0;
    int CurrentCount = 0;
    int LastSpot = 0;
    T[] Array;

    DynaArray(int _size)
    {
        Size = _size;
    }

    @SuppressWarnings("unchecked")
    void push_back(T _element)
    {
        CurrentCount++;
        if(CurrentCount > Size)
        {
            //make a new array for double the size;
            if( Size == 0 )
            {
                Size = 2;
                Array = (T[]) new Object[Size];
            }
            else
            {
                int OldSize = Size;
                Size = Size*2;
                T[] TempArray;
                TempArray = (T[]) new Object[Size];
                int i = 0;
                for( ; i < OldSize; i++ )
                {
                    //Copy over info from Array to TempArray
                    TempArray[i] = Array[i];
                }
                Array = TempArray;
            }
        }

        //Now add the new element onto the array
        Array[LastSpot] = _element;
        LastSpot++;     

    }

    @SuppressWarnings("unchecked")
    void push_front(T _element)
    {
        CurrentCount++;
        if( Size == 0)
        {
            Size = 2;
            Array = (T[]) new Object[Size];
            Array[0] = _element;
            return;
        }
        else
        {
            int OldSize = Size;
            Size = Size+1;
            T[] TempArray;
            TempArray = (T[]) new Object[Size];
            int i = 1;
            for( ; i < OldSize; i++ )
            {
                //Copy over info from Array to TempArray
                TempArray[i] = Array[i-1];              
                Array = TempArray;
                Array[0] = _element;
            }           
    }

    }

    T pop_back()
    {
        if( CurrentCount <= 0)
            return null;
        else
        {
            return Array[CurrentCount-1];
        }

    }

    T pop_front()
    {
        if( CurrentCount <= 0)
            return null;
        else
        {
            return Array[0];
        }
    }

    int size()
    {
        return CurrentCount;
    }

    @SuppressWarnings("unchecked")
    void clear()
    {
        Size = 0;
        CurrentCount = 0;
        LastSpot = 0;
        Array = (T[]) new Object[2];

    }

    @SuppressWarnings("unchecked")
    void removeAt(int index)
    {
        T[] TempArray = (T[]) new Object[Size];
        int ArrayIndex = 0;
        for( int i = 0; i < CurrentCount; i++)
        {
            if( i == index )
                continue;

            TempArray[ArrayIndex] = Array[i];
            ArrayIndex++;

        }
    }

    void remove(T _element)
    {
        for(int i = 0; i < CurrentCount; i++)
        {
            //look for the element
            if( Array[i] == _element)
                removeAt(i);
        }
    }


}

Thanks for any help provided or if my code somehow helped you are welcome

2 Answers2

2

int is a primitive, and as such cannot be used with Java generics. The reason for this is to maintain the bytecode's backwards compatibility (which was important when generics were first introduced). You must use an Integer instead.

N.B. unless you're doing this as an academic exercise, you can/should just use a List<T> instead of your custom-implemented class.

Alternatively: use Trove.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
0

You cannot use Java primitives as a generic type because they are not instances of Object. The reason why int[] works is because a Java array is an object. Use the boxed Integer instead.

Community
  • 1
  • 1
Jeremy
  • 22,188
  • 4
  • 68
  • 81