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

  • Home
  • SEARCH
  • 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 8398245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:00:05+00:00 2026-06-09T21:00:05+00:00

I have the following scenario: I have an existing iterator Iterator<String> it and I

  • 0

I have the following scenario: I have an existing iterator Iterator<String> it and I iterate over its head (say first k elements, which are flagged elements, i.e. they start with ‘*’ ). The only way to know that the flagged elements are over, is by noticing that the (k+1)th element is not flagged.

The problem is that if I do that, the iterator it will not provide me the first value anymore on the next call to next().

I want to pass this iterator to a method as it’s only argument and I would like to avoid changing its signarture and it implementation. I know I could do this:

   public void methodAcceptingIterator(Iterator<String> it) //current signature

   //change it to 

  public void methodAcceptingIterator(String firstElement, Iterator<String> it)

But this looks like a workarround/hack decreasing the elegance and generality of the code, so I don’t want to this.

Any ideas how I could solve this problem ?

  • 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-09T21:00:07+00:00Added an answer on June 9, 2026 at 9:00 pm

    The solution is to create your own Iterator implementation which stores the firstElement and uses the existing iterator as an underlying Iterator to delegate the requests for the rest of the elements to.

    Something like:

      public class IteratorMissingFirst<E> implements Iterator<E>{
    
    private Iterator<E> underlyingIterator;
    private E firstElement;
    private boolean firstElOffered;
    
    public IteratorMissingFirst(E firstElement, Iterator<E> it){
        //initialize all the instance vars
    }
    
    public boolean hasNext(){
        if(!firstElOffered && firstElement != null){
                return true;
        }
        else{
            return underlyingIterator.hasNext();
        }
    }
    
    public E next(){
        if(!firstElOffered){
            firstElOffered = true;
            return firstElement;
        }
        else return underlyingIterator.next();
    }
    
    public void remove(){
    
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following scenario where I want to pass in string and a
I am trying to understand isolation/locks in SQL Server. I have following scenario in
I have the following scenario: in activity A, when a user clicks a button,
I have the following scenario: if(xml.Descendants(ns + Children).FirstOrDefault() != null) { XElement children =
I have the following scenario, a Customer who can have bank accounts in several
I have the following scenario :- template <typename T> class Base { public: virtual
I have the following scenario: Entities are loaded from the database. One of them
I have the following scenario. I have created an ASP.NET web application (framework 3.5)
I have the following scenario. The client code only has access to FooHandler, not
I have the following scenario, class foo { ... private: char *_test; }; void

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.