Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Additional Iterator Interface method

Posted on 2004-10-11
27
281 Views
Last Modified: 2013-12-14
Hi again,
 

   I mentioned earlier that I'm currently implementing my own link list class, and within it, private node and iterator inner classes.
The iterator class implements class Iterator, hence inherits the interfaces hasNext(), next() and remove().

I was wondering whether I can add another method within this inner class and subsequently call it from an outside class - just as you would call the hasNext() and next() methods.

e.g.

private class iterator() {

   public hasNext() {
  }
 
  public next() {
  }

  public remove() {
 }

  /* my method follows */

  public myMethod() {
     return something;
  }



}

I've tried the above code, except myMethod is not recognised.  I don't want this method's code to be implemented in the next() method of the iterator.  

Can this be done? if not, any pointers to the right direction?


Many thanks.

0
Comment
Question by:jenny_kong
  • 9
  • 7
  • 6
  • +2
27 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12275102
You can chain your methods upwards by having a public method that calls a method on your inner class
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12275113
Object o = somethingPublic.callSomethingInner();
0
 

Author Comment

by:jenny_kong
ID: 12275125
Thanks,

   But I need to have this method within my iterator, can this still be done?


Thanks CEHJ
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 37

Expert Comment

by:zzynx
ID: 12275154
>> I've tried the above code, except myMethod is not recognised
What do you mean?

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12275168
>>But I need to have this method within my iterator, can this still be done?

All that need to be done is that your method in the iterator should be called by another method that *is* public, so yes, it can be done
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 34 total points
ID: 12275174
If your iterator is using the java.util.Iterator interface, you cannot add methods.  The Iterator interface is fixed.  You can subclass the Iterator Interface though:

Interface IteratorEX extends java.util.Iterator
{
    public myMethod()
}

...
class MyLinkedList
{
    public IteratorEX getIterator()
    {
           return new MyLinkedList.IteratorImpl(this)
    }

   private class IteratorImpl implements IteratorEX
   {
   public hasNext() {
  }
 
  public next() {
  }

  public remove() {
 }

  /* my method follows */

  public myMethod() {
     return something;
  }

   }
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12275178
public MyIteratorInterface extends Iterator {
     public MyObject myMethod();
}

pubilc class MyIterator implements MyIteratorInterface {

   public boolean hasNext() {
   }
 
   public Object next() {
   }

   public void remove() {
   }

   public MyObject myMethod() {
      // Your styff
   }

}


Now, if you have

MyIterator it = ...;

you can write

      MyObject obj = it.myMethod();
0
 

Author Comment

by:jenny_kong
ID: 12275181
The interfaces for class iterator contains next(); hasNext(); and remove().  I want to add an additional method myMethod and have it assessible outside of the class, just as you would access hasNext() etc.  

public String myMethod() compiles ok, except it cannot be called out the scope of the inner class, since its not an interface method.  Is this really the case???  or must I alter Next() to return more data???


private class iterator() {

   public hasNext() {
  }
 
  public next() {
  }

  public remove() {
 }

  /* my method follows */

  public myMethod() {
     return something;
  }



}

Thanks,.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12275188
>> I want to add an additional method myMethod and have it assessible outside of the class, just as you would access hasNext() etc.
See my previous comment
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 12275196
You need to conform to the Iterator Interface described in the javadocs:
    /**
     * Returns the next element in the iteration.
     *
     * @return the next element in the iteration.
     * @exception NoSuchElementException iteration has no more elements.
    Object next();

The next element is the object stored in your Linked List.  If you want something else, you need to extend the Iterator interface as previously described.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12275222
>>If your iterator is using the java.util.Iterator interface, you cannot add methods.  The Iterator interface is fixed.  

That's not the case actually. That's not how interfaces work. All that is required is that the methods specified in the interface be implemented. You can add as many methods as you want.

I assumed, you were using yyour own iterator class jenny_kong and not implementing java.util.Iterator as it happens
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12275237
???
8°/
0
 

Author Comment

by:jenny_kong
ID: 12275238
Thanks guys...

As per CEHJ, I'm not sure its possible without extending class iterator (as per MongalManic).
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 33 total points
ID: 12275254
jenny_kong - i don't understand why that comment was accepted as the answer - please explain ...
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 33 total points
ID: 12275262
>> i don't understand why that comment was accepted as the answer - please explain ...
I feel the same
0
 

Author Comment

by:jenny_kong
ID: 12275279
Unfortunately, I accidently hit the wrong button, and with this system theres no going back...
Many apologies...I would be happy to divest the points if the system had a better points sharing scheme.


Having read the response If I don't extend the iterator class as per MongalManiac, then It's not possible to define my method within the iterator class - or is there?


I'm very sorry guys, I clicked accept too soon without realising who the comment was from.



0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12275283
>>and with this system theres no going back...

That's actually not true. The entire outcome of the question can be changed
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12275293
Firstly, since we *do* appear to be talking about java.util.Iterator, it is *not* a class, it's an interface. You can do what you require using something like this pattern:

public class IteratorFacade {
      private MyIterator iterator;
      
      
      public Object myMethod() {
            return iterator.myMethod();
      }
      
      
      private class MyIterator implements java.util.Iterator {
            public boolean hasNext() { return true; }
            public Object next() { return null; }
            public void remove() {}
            
            Object myMethod() { return null; }

      }
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12275384
>> Unfortunately, I accidently hit the wrong button, and with this system theres no going back...
>> I would be happy to divest the points if the system had a better points sharing scheme.

1) If you want to reopen this question, post a zero-point question in http://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Reopen
Body: Please reopen this question:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21163377.html

2) After the question has been reopened by an admin, you can re-accept with a point split
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12275401
>> I'm very sorry guys, I clicked accept too soon without realising who the comment was from.
No problem. Just ask to reopen this question. (cf. above)
Or shall we ask it for you?
0
 
LVL 92

Expert Comment

by:objects
ID: 12283512
you can add whatever method you want, to access then however you will need to cast the returned Iterator to whatever it's actual class is:

MyIterator i = (MyIterator) list.iterator();
i.myMethod();
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12543965
>> Many apologies...I would be happy to divest the points if the system had a better points sharing scheme.
The author's intention was a points split between MogalManic, CEHJ and zzynx
0
 

Author Comment

by:jenny_kong
ID: 12550699
Many apologies for the shenanigans / grief caused by this question and the delay in my response.  I am extremely grateful for the assistance received from all (especially CEHJ whom assisted with both questions I've asked with regards to the list container I was writing) who have participated with this thread and the EE's admin for reopening the question.

I maybe wrong here, but I accepted MongelManiac's solution as it was the one I implemented, and seeing through the Iterator Class is defined by default as an interface, it made sense to extend this interface as opposed to wrapping these interfaces with a public class.


Again, many apologies and my sincere thanks!


Jen.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12554087
Thanks for accepting
0
 

Author Comment

by:jenny_kong
ID: 12561308
My hat goes off to your fellas.

Thanks.

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 61
servlet  URL Rewriting 1 42
Best environment for cross platform web development? 5 52
what is the best Integrated development environment 2 19
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question