Solved

Java Iterator next method?

Posted on 2006-10-25
2
1,552 Views
Last Modified: 2008-02-26
In 1.4.2


Iterator itr = ...;

itr.next() gets the next object and advances the iterator?  How do I get it so that it only advances when I want it to.

For instance I need to retrieve the value referenced by the iterator several times during this block of code.

0
Comment
Question by:polkadot
2 Comments
 
LVL 14

Expert Comment

by:hoomanv
ID: 17803401
Some collection use ListIterator which enables previous() as well as next()
You can get the element with next() and then rewind it by previous()
0
 
LVL 6

Accepted Solution

by:
valipotor earned 500 total points
ID: 17803454
Hi
Why don't you store the retrieved object?

Like this:

Object obj=itr.next();

//use obj from now on

obj.method1();

...

method2(obj)

It is recommanded that you do this way, the next() method has it's own overhead.

Best regards,
valipotor
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question