Solved

Return an Iterator Object

Posted on 2003-12-04
13
469 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
 
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
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.

 
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

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 68
solarwind tftp server 2 44
Java DateChooser? 3 29
servlet example issue 6 29
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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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

12 Experts available now in Live!

Get 1:1 Help Now