Solved

Return an Iterator Object

Posted on 2003-12-04
13
467 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:mayankeagle
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
 
LVL 7

Accepted Solution

by:
grim_toaster earned 125 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 64
json format text only 4 65
wordlen challenge 3 47
Java Timer (static) 9 16
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

758 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

18 Experts available now in Live!

Get 1:1 Help Now