Solved

Return an Iterator Object

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

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…
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 …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

734 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