aspose file tools
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
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: 3370

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: 3370

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: 19956

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
It is sorta covered in the JavaRanch Style Guide.
subject: for loop and the list iterator