File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Using Iterator.remove() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using Iterator.remove()" Watch "Using Iterator.remove()" New topic
Author

Using Iterator.remove()

Haakon Lind
Greenhorn

Joined: Sep 18, 2002
Posts: 22
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:

PS! the two calls to hand[3] is always the same.
Thanks for any help!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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
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

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Iterator.remove()
 
Similar Threads
FilteredIterator
ArrayList
Multiple thread accessing List (ArrayList, Vector)
null check in Iterator
Making a String[] from ResultSet