Solved

Java Iterator next method?

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

740 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