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