Solved

Java Iterator next method?

Posted on 2006-10-25
2
1,550 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now