?
Solved

Java Iterator next method?

Posted on 2006-10-25
2
Medium Priority
?
1,558 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

719 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