[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

Additional Iterator Interface method

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
jenny_kong
Asked:
jenny_kong
  • 9
  • 7
  • 6
  • +2
3 Solutions
 
CEHJCommented:
You can chain your methods upwards by having a public method that calls a method on your inner class
0
 
CEHJCommented:
Object o = somethingPublic.callSomethingInner();
0
 
jenny_kongAuthor Commented:
Thanks,

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


Thanks CEHJ
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
zzynxSoftware engineerCommented:
>> I've tried the above code, except myMethod is not recognised
What do you mean?

0
 
CEHJCommented:
>>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
 
MogalManicCommented:
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
 
zzynxSoftware engineerCommented:
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
 
jenny_kongAuthor Commented:
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
 
zzynxSoftware engineerCommented:
>> 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
 
MogalManicCommented:
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
 
CEHJCommented:
>>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
 
zzynxSoftware engineerCommented:
???
8°/
0
 
jenny_kongAuthor Commented:
Thanks guys...

As per CEHJ, I'm not sure its possible without extending class iterator (as per MongalManic).
0
 
CEHJCommented:
jenny_kong - i don't understand why that comment was accepted as the answer - please explain ...
0
 
zzynxSoftware engineerCommented:
>> i don't understand why that comment was accepted as the answer - please explain ...
I feel the same
0
 
jenny_kongAuthor Commented:
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
 
CEHJCommented:
>>and with this system theres no going back...

That's actually not true. The entire outcome of the question can be changed
0
 
CEHJCommented:
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
 
zzynxSoftware engineerCommented:
>> 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
 
zzynxSoftware engineerCommented:
>> 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
 
objectsCommented:
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
 
zzynxSoftware engineerCommented:
>> 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
 
jenny_kongAuthor Commented:
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
 
zzynxSoftware engineerCommented:
Thanks for accepting
0
 
jenny_kongAuthor Commented:
My hat goes off to your fellas.

Thanks.

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 9
  • 7
  • 6
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now