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 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
Author

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

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

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 - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop and the list iterator
 
Similar Threads
problem with advance for loop
Regarding TypeSafe Iterator
Generics Question
My code compiled well in JDK1.4 but warning in JDK1.5.
For-eaching an iterator