Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7941657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:44:06+00:00 2026-06-03T23:44:06+00:00

Is it advisable not to use iterator.hasNext() in looping over an iterator? For example

  • 0

Is it advisable not to use iterator.hasNext() in looping over an iterator?

For example I would like to set value obj to each element of a list. I could use the following code or make it more readable by using hasNext() in a loop.

int size = list.size();
ListIterator<? super T> itr = list.listIterator();
for (int i=0; i<size; i++) {
     itr.next();
     itr.set(obj);
}

Instead of these lines I could write my code like the following.

for (ListIterator<? super T> itr = list.listIterator(); itr.hasNext(); ) {
    itr.next();
    itr.set(obj);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:44:08+00:00Added an answer on June 3, 2026 at 11:44 pm

    Is it advisable not to use iterator.hasNext() in looping over an iterator?

    Um, no. hasNext is the standard way you iterate with an iterator. That’s what the enhanced-for statement does behind the scenes for an iterable, for example.

    Having said that, your code is already ListIterator-specific, as you’re using ListIterator.set – so your second block of code won’t actually compile at the moment. Even if it did, it wouldn’t work, as you still need to call next(). This would work though:

    for (ListIterator<? super T> itr = list.listIterator(); itr.hasNext(); ) {
        itr.next();
        itr.set(obj);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read that system(pause) is slow and not advisable to use. Is there any
Why is it not advisable to use JavaScript in JSP? One rationale that I
After reading several other questions, it looks like it's not advisable to have an
I would like to include an external website (e.g. www.example.com) in a div in
Is it possible (or even advisable) to cast the element retrieved from a for
Is it possible (or even advisable) to use a PriorityExecutorBasedEventDrivenDispatcher with a TypedActor, in
I guess I'm wondering if it is ill-advisable to just set: div { overflow:
I'm interested in compiling a list of c++ features that are not advisable for
I'd like to use the Razor engine without view (cshtml) files, but on strings.
Is it advisable to use Session variables to pass data between aspx pages on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.