I have a project I have been assigned in java and I have to basically create my own class that mimics basic features of the ArrayList class.
I have to do it in the format shown below using these variables but I am stuck on one part. When the array fills up I want to make a new one with 10 extra spaces, then copy all the old array elements to the new one and use that from now on. How do I make my program automatically use this array from now on? Because it will continue to use the first array.
public MyArrayList()
{
array = new String[10];
arraySize = 10;
arrayElements = 0;
}
public void add (String string)
{
if (arrayElements < arraySize)
{
array[arrayElements] = string;
arrayElements++;
} else
{
arraySize += 10;
arrayElements++;
String[] array2 = new String[arraySize];
for (int i = 0; i < arrElements;i++)
{
array2[i] = array[i];
}
//missing code here I think?
array2[arrayElements] = string;
}
}
arrayElements++;should come after you copy #arrayElements over to a new array.