I am experimenting with the system.arraycopy() method, for use in homework excerices.
i need to implement a an array that grows in size as and when new objects are added. Normally i would use a linked list etc, however my teacher forbids it. I can only use arraycopy().
I wrote a small class to practice using arraycopy():
public class ArrayTest {
int array1[] = new int[2];
int array2[] = new int[3];
int size;
public ArrayTest()
{
array1[0] = 6;
array1[1] = 6;
size = 0;
}
public void testArrays()
{
System.arraycopy(array1, 0, array2, 0, 2);
}
public void display()
{
for(int i = 0;i<array2.length;i++)
{
System.out.println(array2);
}
}
public static void main(String argrs[])
{
ArrayTest a = new ArrayTest();
a.testArrays();
a.display();
}
}
I see how one can add the contents of a smaller array to a larger one when to gain another space, but this is still a finite solution.
is there a way i can code a infinte solution and would it be ok if I could look at an example?
Thank You For Your Time.
P.S. I am aware that this problem was raised in another thread but i thought that thread was becoming too long.