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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:23:50+00:00 2026-06-05T16:23:50+00:00

Although this code is from an algorithm text, I have a bit of a

  • 0

Although this code is from an algorithm text, I have a bit of a trouble regarding nested classes and interfaces – actually, 90% of my confusion arises from how this code implements the interface. Again, this question is not about the algorithm itself.

As I understand, this code uses a nested class so that it can access the private instance variables in ResizingArrayStack (this text uses a convention to declare all instance variables private for encapsulation).

The Iterable interface is this:

public Iterator<Item> { Iterator<Item> iterator(); } // ignore the quotes

and the Iterator interface is this:

public interface Iterator<Item> { boolean hasNext(); Item next(); void remove(); }

And my question is how all these connect in the code shown below.

The parent class implements the Iterable interface, but where is the Iterator interface coming directly from when ReverseArrayIterator implements Iterator? Is it coming from the Iterator instance method, or from the Iterable interface? Intuition tells me that it’s implementing directly from the Iterator instance method and ultimately from the Iterable interface (kind of like how extends work?).

Sorry for my lack of OOP knowledge..this text only briefly talks about it, and I was told that I didn’t have to know any of this (and I probably don’t, as long as I understand the algorithms), but I HAVE TO UNDERSTAND IT lol. I just can’t get this off my mind. Thanks in advance.

// from http://algs4.cs.princeton.edu/13stacks/ResizingArrayStack.java.html
import java.util.Iterator;
import java.util.NoSuchElementException;

public class ResizingArrayStack<Item> implements Iterable<Item> {
    private Item[] a;         // array of items
    private int N;            // number of elements on stack

    // create an empty stack
    public ResizingArrayStack() {
        a = (Item[]) new Object[2];
    }

    public boolean isEmpty() { return N == 0; }
    public int size()        { return N;      }



    // resize the underlying array holding the elements
    private void resize(int capacity) {
        assert capacity >= N;
        Item[] temp = (Item[]) new Object[capacity];
        for (int i = 0; i < N; i++) {
            temp[i] = a[i];
        }
        a = temp;
    }

    // push a new item onto the stack
    public void push(Item item) {
        if (N == a.length) resize(2*a.length);    // double size of array if necessary
        a[N++] = item;                            // add item
    }

    // delete and return the item most recently added
    public Item pop() {
        if (isEmpty()) { throw new RuntimeException("Stack underflow error"); }
        Item item = a[N-1];
        a[N-1] = null;                              // to avoid loitering
        N--;
        // shrink size of array if necessary
        if (N > 0 && N == a.length/4) resize(a.length/2);
        return item;
    }


    public Iterator<Item> iterator()  { return new ReverseArrayIterator();  }

    // an iterator, doesn't implement remove() since it's optional
    private class ReverseArrayIterator implements Iterator<Item> {
        private int i = N;
        public boolean hasNext()  { return i > 0;                               }
        public void remove()      { throw new UnsupportedOperationException();  }

        public Item next() {
            if (!hasNext()) throw new NoSuchElementException();
            return a[--i];
        }
    }

}
  • 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-05T16:23:51+00:00Added an answer on June 5, 2026 at 4:23 pm

    The parent class implements the Iterable interface, but where is the Iterator interface coming directly from when ReverseArrayIterator implements Iterator?

    That question doesn’t make much sense. The Iterator interface “comes from” the Java SE class libraries (‘cos your code imports it), and it is implemented by the ReverseArrayIterator class. And at runtime, a call to the ResizingArrayStack.iterator() method creates an instance of the ReverseArrayIterator class when you call it.

    Intuition tells me that it’s implementing directly from the Iterator instance method and ultimately from the Iterable interface (kind of like how extends work?).

    The connection with the Iterable interface is that the outer class implements it. But Iterable just “means” that there is an iterator() method that can be called to create an Iterator instance of the appropriate type. There is no special “kind of like how extends work” magic happening. It is all just classes implementing interfaces in this example.

    The other “different” thing about this is that ReverseArrayIterator is a nested class, and can therefore access the private state of the parent object. In this case a and N.

    What is going on here is that a number of independent things (language features and design patterns) are being combined to achieve a particular overall effect. That overall effect is to make it possible to iterate the elements of the stack using a “for each” loop.

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

Sidebar

Related Questions

Although there are a lot of jquery autocomplete code in this forum. However I
This is a follow on from my previous question although this is about something
I have the SQL2008 version of SMO and although it uses this class internally
Although I have my php validation doing this, thought I may as well pop
I have this linq query that works well (although it may be written better,
I was using the topcoder C++ compiler, and although this code just run fine
While implementing the code from this question on my project I realized there's 3
This is my code taken from http://jqueryui.com/demos/draggable/ <!DOCTYPE html> <html lang=en> <head> <meta charset=utf-8>
I have these 3 tables: and I'm using this code to join them together
I have been having trouble keeping the canvas/window from just expanding to the size

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.