Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8827737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:26:36+00:00 2026-06-14T07:26:36+00:00

I have a class ‘Deck’, that creates an ArrayList called deck. I’m trying to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:26:38+00:00Added an answer on June 14, 2026 at 7:26 am

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class that create a lot of other classes. Is good idea to
I have class that extend FragmentActivity in it I add fragment to layout as
I have class grades that I need to check if a specific grade is
I have class that looks like this: class A { public: class variables_map vm
I have class B that has class A, this is simplified version of class
I have class GUI so I can create object like this: GUI g1 =
I have class instantiated in a web service that, in a static member, holds
I have class that extends BroadcastReceiver and gets all new sms. When I get
I have class User that contains protected constructor and class Account that has access
I have class dot and have html code like that: <span class=dot>Text1</span> <span class=dot>Text2</span>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.