Solved

Java Iterator next method?

Posted on 2006-10-25
2
1,554 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String array comparison 4 57
sql import cannot be resolved jsp 3 116
Java Restore security prompts not working 10 259
restrict decimal places for double datatype 10 49
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

734 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