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 8930535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:54:50+00:00 2026-06-15T08:54:50+00:00

My framework is full of iterators like int n = col.getSize(); for (int i

  • 0

My framework is full of iterators like

int n = col.getSize();
for (int i = 0 ; i != n; i++) {
  Type obj = col.get(i);

And, I feel that every program that iterates items over the index has a piece of code like this. It feels ugly compared to the neat foreach

for (Type obj : col) {

Transition to foreach requires making all the collections Iterable, which means creating a new Iterator in every iterator() method. Now, I had the ugly code in every collection declaration instead of its use places. So, I did the a good job, I moved the ugly code into the common.utils.

public abstract class ImmutableIterator<T> implements Iterator<T> {
    private int i = 0; 
    private final int size = getSize();

    public abstract int getSize();  // to be implemented by user
    public abstract T getValue(int i); // to be implemented by user

    public T next() { 
        if (!hasNext()) throw new NoSuchElementException(); 
        return getValue(i++);
    }

    public boolean hasNext() { return i != size;}

    public void remove() {
        throw new UnsupportedOperationException("Remove is not implemented");
    }
}

Now, I have isolated this ugly pattern in only place. This is the best I could do. The collections will supply the size and value(i). The only problem is performance penaly: I have to do the size check twice in the loop, first for hasNext and another in the next(). I wonder why Sun does not provide such a class? It is needed to everybody. Have I just overlooked it?

  • 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-15T08:54:51+00:00Added an answer on June 15, 2026 at 8:54 am

    Tomasz Nurkiewicz directed us to the Guava’s UnmodifiableIterator. But, only remove method is defined there whereas we want the index iteration also. This hint allowed LouisWasserman to find out a better solution: the AbstractIndexedListIterator does what we want. The only drawback is that this class does not implement the remove method and is closed in the Guava package. All this tells us that such classes are really useful but must be implemented by the user in his framework.

    JB Nizet proposed another solution. Instead of implementing the iterator interface on top of getSize/GetItem(idx) methods, we can return one retreived from the underlying collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in a full .NET framework solution: public delegate int
is there a full-featured framework for C that is similar to Qt ?
Does silverlight provide a mechanism like full .NET framework's AppDomain for loading and unloading
I know that to get access to a configured twitter account using twitter framework
I am developing a C# (targeting full framework 4) windows app. I need that
I'm working on a set of components (that will hopefully become a full framework)
I want to integrate a full HTML framework (ie HTML/CSS/JavaScript) inside an iOS app
Our framework requires wrapping certain functions in some ugly boilerplate code: def prefix_myname_suffix(obj): def
.NET framework 3.5 introduces Action and Func<> predefined delegate types that are very handy.
Based on the http://framework.zend.com/download/latest Zend Framework + Zend Server Community Edition(CE) Zend Framework Full

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.