1

I have a class 'Deck', that creates an ArrayList called deck. I'm trying to create a nested Iterator class that traverses the Cards in reverse order.

public class Deck {

    //Nested Iterator class to traverse the Cards in reverse order
    public abstract class DeckIterator implements Iterator<Card>{

        ListIterator it = deck.listIterator(deck.size());

        //Iterate in reverse.
        while(it.hasPrevious()) { //errors
            //System.out.println(it.previous());
            return it.previous();
        }  
    }
}

I have tried the suggestions below but I am still having no luck... Instead of copying One ArrayList to another in reverse order, i'd rather Iterate over the existing ArrayList in the Outer Class. What is the most efficient way of doing this?

edwoollard
  • 12,245
  • 6
  • 43
  • 74
binary101
  • 1,013
  • 3
  • 23
  • 34

3 Answers3

4

Firstly make Deck implement Iterable<Card>, this will require you to implement the iterator() method. Make that method return an instance of your nested DeckIterator class.

@Override
public Iterator<Card> iterator()
{
    return new DeckIterator(deck);
}

Then make DeckIterator implement Iterator<Card> and implement the hasNext(), next() and remove() methods.

private static class DeckIterator implements Iterator<Card>
{

private int nextCard;
private final List<Card> cards;

public DeckIterator(List<Card> cards)
{
    this.cards = cards;
    this.nextCard = cards.size() - 1;
}

@Override
public boolean hasNext()
{
    if (nextCard < 0)
        return false;
    return true;
}

/**
 * {@inheritDoc}
 */
@Override
public Card next()
{
    if (hasNext())
        return cards.get(nextCard--);
    return null;
}

/**
 * {@inheritDoc}
 */
@Override
public void remove()
{

}
}

Then use,

Iterator<Card> iterator = yourDeck.iterator();
while (iterator.hasNext())
{
 Card card = iterator.next();
}

To iterate backwards over the deck.

However you dont need to create your own Iterator if you just want to iterate in reverse ListIterator can do it for you.

ArrayList<Card> deck = new ArrayList<Card>();
// Do whatever you do with your deck :P

ListIterator<Card> li = deck.listIterator(deck.size());

// Iterate in reverse.
while(li.hasPrevious()) 
{
  Card card = li.previous();
  // Do stuff with the card
}
The Cat
  • 2,375
  • 6
  • 25
  • 37
  • This is what i was aiming for except the 'while' errors with "illegal start of type, package li does not exist, expected, ';' expected, missing return statement – binary101 Nov 14 '12 at 16:20
  • updated my answer, now the iterator should iterate in reverse – The Cat Nov 14 '12 at 16:56
0

Iterator is an interface, and as such you need to implement the methods in it. See this SO question for more information.

However, perhaps the simplest thing is to create a method in your Deck that

  1. creates a copy of the collection but in reverse order
  2. returns an iterator over that collection

Note that since you're now iterating over a copy of the original collection it should be unaffected by another thread adding/removing cards from the desk.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • might want to add the text of the solution. There have been a good deal of posts getting flagged/removed for "link only" answers – Woot4Moo Nov 14 '12 at 15:56
0

With Guava you can reverse the list and iterate over the result. Look at: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Lists.html#reverse%28java.util.List%29

That should be easy enough and shouldn't cause to much changes/refactoring.

Jordi Laforge
  • 670
  • 3
  • 19