Solved

Additional Iterator Interface method

Posted on 2004-10-11
27
276 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now