Using Iterator.remove()

When running this shuffler program I expect to get five random objects and then a null at the end. But I`m getting six objects in output when I got 5 declared. I think it`s because I don`t know how to use the Iterator.remove() method properly. Here`s the code:

Hi Haakon, The iterator is working fine. The problem is that hand[3] still holds the value from the previous assignment. Try using hand[size + 3] in the second loop and dump hand[4], hand[5] and hand[6] instead and see what happens. Michael Morris

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher

Haakon Lind
Greenhorn

Joined: Sep 18, 2002
Posts: 22

posted Sep 27, 2002 10:12:00

0

Thanks Michael, didn`t see that it couldn`t cast a null as it skips the last code on hand[3] and thus the old object is still in place. Got a ....OutOfBoundsException when i tried your tip, but I threw in this at the end instead:

So now it works Thanks again, Haakon

Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

posted Sep 27, 2002 12:50:00

0

Got a ....OutOfBoundsException when i tried your tip, but I threw in this at the end instead:

That's why I told you to do it that way so you could see what was going on, ie that the second loop only had two iterations, otherwise the Exception would have occurred in the loop instead of the Sysout. Michael Morr