This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes for loop and the list iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for loop and the list iterator" Watch "for loop and the list iterator" New topic

for loop and the list iterator

vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Posts: 76
how can I use the for loop with the list iterator to be able to use the trim method, illustrate by example on this program

Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

I don't get your doubt. Have you tried running the program? It has the for loop to use the list.

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

The program seems to use the list object and use strings inside it.

You can directly use the trim() method on 'a' right?
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
You may be confusing String.trim() and Iterator.remove().

You would want access to the iterator to remove items from the list. Invoking trim on a string will return a new string instance that has been trimmed (white space removed from the front and back).

To get access to the iterator, you use the old form (pre Java 5) of the for loop.

Bill Shirley - bshirley -
if (Posts < 30) JavaRanchFAQ);
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

Whenever you use the new for loop, you lose the iterator or array indexes. It's a price you pay for the shorter code.

So if you need the iterator to remove items, the array index (e.g. to copy array items) or a list iterator (because of the add and set methods), there is no other option but to use the old for loop.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: for loop and the list iterator
jQuery in Action, 3rd edition