Solved

Return an Iterator Object

Posted on 2003-12-04
13
478 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
[X]
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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses

622 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