0

First of all, and in order for you to understand this question, i'm going to briefly explain my project:

I have a class named Pair, which basically creates the type

Pair<String,Double>

and has a getFirst() and getSecond() methods for returning the String and Double values respectively.

Then i have another class, named Package, which basically consists in a list of Pairs, and implements the Iterable interface, so i can iterate trough the list:

Package<Pair<String,Double>> package;
List <Pair<String,Double>> list;

What i want is to sum the doubles on each Pair, using the iterator().

The iterator is defined like this for the Package class:

public Iterator<E> iterator() {

        return this.iterator();
    }

I've tried two different approaches, which in both cases resulted in a:

Exception in thread "main" java.lang.StackOverflowError
    at Package.iterator(Package.java:98)

Here's the first one:

public static double packageWeight(Package<Pair<String, Double>> package) {

    double sum = 0;
    Pair<String, Double> pair;

    while (package.iterator().hasNext()) {

        pair = package.iterator().next();
        sum = sum + pair.getSecond();

    }

    return sum;
}

And the second one:

public static double packageWeight(Package<Pair<String, Double>> package) {

    double sum = 0;
    Pair<String, Double> pair;

    Iterator<Pair<String,Double>> it = Package.iterator();

    while (it.hasNext()) {

        pair = it.next();
        sum = sum + pair.getSecond();

    }

    return sum;
}

My question is: What am i doing wrong in order to get this error?

5
  • Can you post the rest of your code? Commented Apr 7, 2015 at 14:05
  • Also, don't you need to pass the iterator an object to iterate over? In the packageWeight method... Commented Apr 7, 2015 at 14:05
  • 2
    You can't have a variable called package, surely? It's a java keyword. Commented Apr 7, 2015 at 14:06
  • You also can't be invoking Package.iterator() unless you make it static. Commented Apr 7, 2015 at 14:11
  • 3
    The first code is fundamentally wrong anyway. You'll always get a new iterator (at least if its correctly implemented), so you should have an infinite loop. Also since you perform a read-only operation, let your class implement the Iterable interface and use a for-each. That will be less error prone... Commented Apr 7, 2015 at 14:11

2 Answers 2

5

The immediate cause of the StackOverflowError is that you are invoking the method in itself:

public Iterator<E> iterator() {

        return this.iterator();
    }

You need to invoke a different method here, e.g. return list.iterator() or package.iterator() (except that package is not a valid identifier in Java, since it is a keyword).

Sign up to request clarification or add additional context in comments.

2 Comments

yes! Indeed the problem was solved using my second approach and changing the return status to list.iterator(), thanks!
about the package.iterator() problem, i was just translating the question to english, and it turned out to look like i had a problem there! the original name is different though
2

Your first attempt is flawed because you are requesting a new iterator each time you call package.iterator() - I think you knew that because you then wrote the second one.

The fault with the second is more subtle - you need to use package.iterator() not Package.iterator(). The second should not work anyway.

And what Andy Turner said.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.