Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

system.arraycopy() issues

807569Jun 21 2006 — edited Jun 21 2006
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 19 2006
Added on Jun 21 2006
49 comments
522 views