Claudio Pomo
Claudio Pomo

Reputation: 2472

Iterator java nested loop

I've a question: there is a way to increment an iterator directly into nested loop? Example

while(budgets.iterator().hasNext()){
    if(budgets.iterator().next().getTipoBudget()!="I"){
        if(budgets.size()<maxInviate){
            for(i=0; i<maxInviate ;i++){
                if(budgetArray[i]==0 && budgets.iterator().hasNext()){
                    budgetArray[i] = budgets.iterator().next().getBudgetField();

                }
            }
            i=0;
            Set budgetSet = new HashSet<Integer>(Arrays.asList(budgetArray));
            Integer min = Collections.min(budgetSet);
            valueAdw = valueAdw + min;
            for(i=0; i<maxInviate; i++){
                budgetArray[i] = budgetArray[i] - valueAdw;
            }
        }else{
            valueAdw = null;
            valueAdwEnd = null;
            campaigns.addLast(new Campaign(valueAdw, valueAdwEnd, (long) adwordsBudget.getZona().getAdwordsCode()));
        }
    }
}

Upvotes: 1

Views: 3894

Answers (1)

tibtof
tibtof

Reputation: 7957

The problem is that budgets.iterator() returns a new Iterator every time, so budgets.iterator().hasNext() moves the pointer in an Iterator that you're not using any more. Define

Iterator iterator = budgets.iterator();

and use that in the rest of the code.

Also @Sachin J is right, you have to write iterator.next() in the else block, too. Also @Marko Topolnik sorry I just saw your comment, I didn't try to steal your idea.

Upvotes: 1

Related Questions