aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes The New for Loop, Iterators and Unsupported Operation Exceptions on List.remove() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "The New for Loop, Iterators and Unsupported Operation Exceptions on List.remove()" Watch "The New for Loop, Iterators and Unsupported Operation Exceptions on List.remove()" New topic
Author

The New for Loop, Iterators and Unsupported Operation Exceptions on List.remove()

Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 193
I'm playing around with the new Java 5 for loop and trying to make sure I understand any differences between it and using an Iterator to deal with each element of a collection for SCJP 5.0. I read somewhere (don't remember where) or maybe heard in a talk at JavaOne, that said that there were certain restrictions or cases where you could NOT use the new for loop, but you could use an Iterator. Specifically, I think it was in cases dealing with Unsupported Operation exceptions caused by modifying the list (e.g. doing a remove).

I've read The For-Each Loop article that is part of J2SE(TM) 5.0 New Features. It contains the following quote, but I'd still like to know more.


So when should you use the for-each loop? Any time you can. It really beautifies your code. Unfortunately, you cannot use it everywhere. Consider, for example, the expurgate method. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally, it is not usable for loops that must iterate over multiple collections in parallel. These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that would cover the great majority of cases.


Could anyone suggest some good reading material that the following questions:

A) What are the differences between the new for loop and Iterators? When should each of them be used?

B) What are the specific cases where removing from a List will give an Unsupported Operation exception? Do the new for loops and Iterators behave the same way with regard to this?

Thanks,
Josh


Rational Pi Blog - Java, SCJP, Dev Bits- http://rationalpi.wordpress.com
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Good question. I think the quote alone really emcompasses the difference and when you would choice one over the other.

For UnsupportedOperation, both will behave the same, if you are looping through a collection, Iterator or not, and call the Collections remove, then you will get this exception. If you use the new For-Each loop, you do not have access to the Iterator, so you cannot call the only way to delete from the actual Collection, which is through the Iterators delete method.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 193
Thanks Mark.

So I guess it comes down to whether you need access to the Iterator. If not, use a new for loop, else, do it the old fashion way.

Again thanks,
Josh
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Exactly.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The New for Loop, Iterators and Unsupported Operation Exceptions on List.remove()