Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Return an Iterator Object

Posted on 2003-12-04
13
Medium Priority
?
483 Views
Last Modified: 2010-03-31
Having a problem returing a Iterator object
Looking through an ArrayList then returing that Iterator object

public void addAllAtBottom (Filofax other)
    {
      Iterator e = other.iterator();
      while(e.hasNext())    <------------------ Problem is here it seems to skip this bit see below
      {
        Card card = (Card) e.next();
        list.add(card);
      }
    }

public Iterator iterator ()
    {
      Iterator e = list.iterator();

      while (e.hasNext()) {
       e.next();
      }
      return (Iterator)e;
    }

What i seem to be returning is an abstract Iterator ...whoosh over my head  i dont think the problem lies with the top method but within the e.HasNext of the iterator() method

Thank yo in advance!!
0
Comment
Question by:dinger46
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 9873496
Are these two methods inside the same class (Filofax)? Also, the while loop in your iterator () method seems to do nothing useful at all. Which class is Filofax extending?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9873519
You can't pass an abstract Iterator, because an Object derivate is by definition NOT abstract.
Your "return(Iterator)e;"   should be "return(e);" because you are referring to the same thing already.

The whole thing comes back to the "list.iterator()" method that will return you a real Iterator.

Is the "e.hasNext()" in the upper method in contradiction to the code in the lower method?

It's not clear to me how you connect the 2 methods here.
;JOOP!

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9873566
If Filofax is a collection of Card as your code seems to suggest, it will already have an iterator() method. You can probably remove that one you have inserted into the calling class. If the calling class *is* Filofax then it will call the iterator() method you have installed and will produce the effect you mention below:

>>Problem is here it seems to skip this bit see below

Otherwise, one reason for that could be that there aren't any Card ;-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
grim_toaster earned 500 total points
ID: 9873587
Or alternatively change your iterator method to remove the calls next() calls.  Normal Iterators only allow you to iterate over each element once, therefore when you call next() on every item in the iterator, you are effectively going to the end, so anything that later uses it will say that there are no more elements

// change iterator() method...
public Iterator iterator ()
    {
      return list.iterator();
    }
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9873597
Or as another approach, do not deal with the iterators, simply add all of the elements from one collection (presumption here) to the other, using Collection's addAll(Collection) method referencing the other objects list.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9873601
No need to hide the iterator() method that's probably *already* there ;-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9873607
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9873636
Yes - thinking about that one - don't know whether I'd have much of use to contribute ;-)
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9873677
Well at least you get asked!  Instead of those of us not on the top 15!  ;)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9873731
I'm sure he'd like to hear from you too! btw, dinger, the comment made at Date: 12/04/2003 03:26AM PST is one of the best yet.
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9873894
Would that be my one or your one???
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9873923
grim_toaster, that question is for everyone to participate. I have also kept links to that question all 5 other sections, viz., Java, C, C++, Programming Languages and Visual Basic. Anyways, I'd better stop clogging this page :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9873994
>>Would that be my one or your one???

Well it can't be mine - unless we have different date views of this page ;-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

927 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