problem shifting (copying) array elements in a list

I am trying to add points to the beginning of a list. However, I cannot seem to debug the insertBeginning( ) method. I am faced with a problem that overwrites certain array elements. If I can get any help I would greatly appreciate it.

Thanks,

I have included a sample run and some code.

The following url contains both the main and the class files.

http://lbbmx.com/java/

publicvoid appendZ ( Point newPoint )// used w/ insertBeg method

{

size++;

cursor=0;

ptlist[cursor]= newPoint;

}

publicvoid insertBeginning ( Point newPoint )// Insert begin.

{

if(this.isEmpty())

{append(newPoint);}

else

{

cursor=0;

for(int i=size; i>cursor; i--)

{

ptlist[size]= ptlist[size-1];//pt[1]=pt[0]

}

this.appendZ(newPoint);

Here is a sample run:

Commands:

+ x y : Append point (x,y) to the end of the list

# x y : Insert point (x,y) at beginning

Q: Quit the test program

Empty list

Command: +1 1

Append (1,1)

size = 1cursor = 0

01234567

(1,1)

Command: #2 2

Insert (2,2) at beginning

size = 2cursor = 0

01234567

(2,2)(1,1)

Command: #3 3

Insert (3,3) at beginning

size = 3cursor = 0

01234567

(3,3)(1,1)(1,1)

[1949 byte] By [dhzbmxa] at [2007-11-27 4:43:56]
# 1

ptlist[size]= ptlist[size-1];

In this situation, size is the variable you are keeping track of how many elements are in your array. This does not change until after you have inserted the new Point. So, regardless of how many times you go around the loop you are simply copy the same value.

"How do I fix it?" I hear you ask.

Well what changes everytime you go around the loop?

floundera at 2007-7-12 9:55:46 > top of Java-index,Java Essentials,Java Programming...
# 2
"i" is the magic variablerevision:ptlist[i]= ptlist[i-1]Thank you very much.
dhzbmxa at 2007-7-12 9:55:46 > top of Java-index,Java Essentials,Java Programming...