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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:13:04+00:00 2026-05-27T05:13:04+00:00

Is the interface Iterator already defined somewhere in java library (mind the terminology). i.e.

  • 0

Is the interface Iterator already defined somewhere in java library (mind the terminology).
i.e.

What i am asking is that, say i have an arraylist, now i write.

Iterator itr= new Iterator();

but i never define anything like

public interface Iterator{  // all the methods };

Do i need to import some package where this iterator is already defined?

Let me take an example here:

class BOX implements Comparable {

    private double length;
    private double width;
    private double height;

    BOX(double l, double b, double h) {
        length = l;
        width = b;
        height = h;
    }

    public double getLength() {
        return length;
    }

    public double getWidth() {
        return width;
    }

    public double getHeight() {
        return height;
    }

    public double getArea() {
        return 2 * (length * width + width * height + height * length);
    }

    public double getVolume() {
        return length * width * height;
    }

    public int compareTo(Object other) {
        BOX b1 = (BOX) other;
        if (this.getVolume() > b1.getVolume()) {
            return 1;
        }
        if (this.getVolume() < b1.getVolume()) {
            return -1;
        }
        return 0;
    }

    public String toString() {
        return 
        “Length:
        ”+length +
        ” Width:
        ”+width +
        ” Height:
        ”+height;
    }
} // End of BOX class

And here is my test class.

import java.util.*;

class ComparableTest {

    public static void main(String[] args) {
        ArrayList box = new ArrayList();
        box.add(new BOX(10, 8, 6));
        box.add(new BOX(5, 10, 5));
        box.add(new BOX(8, 8, 8));
        box.add(new BOX(10, 20, 30));
        box.add(new BOX(1, 2, 3));
        Collections.sort(box);
        Iterator itr = ar.iterator();
        while (itr.hasNext()) {
            BOX b = (BOX) itr.next();
            System.out.println(b);
        }
    }
}// End of class

Now in class ComparableTest should not it implement interface iterator also, shall i not define an interface iterator that will contain all the methods. Also, where is the implementation of the iterator methods are?

I maybe confused alot, but kindly help!
thanks.

  • 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-05-27T05:13:05+00:00Added an answer on May 27, 2026 at 5:13 am

    I suspect you mean java.util.Iterator<E>.

    And no, you don’t write:

    Iterator itr= new Iterator();
    

    … that could never work, given that it’s an interface. Also, it’s Iterator, not iterator, and your code should use import, not Import – Java is case-sensitive.

    Instead, you write:

    Iterator<Foo> iterator = list.iterator();
    

    But no, ComparableTest doesn’t need to implement Iterator<E> – why would it? It uses the Iterator interface, but it doesn’t implement it.

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

Sidebar

Related Questions

Why is there the method iterator() defined on the interface java.util.Collection when it already
I have a class MyDoublyIterator<E> that implements the Iterator<E> interface. Also, it has a
I have an implementation of java.util.Iterator which requires that the call to next() should
I have different types, say A , B , C , that all inherit
What is Iterator and collections? Does these two have any relations? // the interface
i have this snippet of code that use an iterator on a list for
I have an iterator class that loops over an array of objects and lazily
I am doing a research on java iterator interface and cannot understand why it
I have an object that implements ArrayAccess , Iterator and Countable . That produces
I have class, that contains different types and implements Iterable<Object> interface. But it is

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.