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

  • SEARCH
  • Home
  • 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 9158189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:11:26+00:00 2026-06-17T13:11:26+00:00

I’ve been trying to make a class to represent a deck of cards. However,

  • 0

I’ve been trying to make a class to represent a deck of cards. However, I wanted to create it in a way which it could be any kind of cards, it doesn’t need to know which kind it just has be able to store them, shuffle and draw them one at a time be them uno cards, regular playing cards, or trading cards. For this, I’ve been trying something I’ve heard of but have not used — Generics.

However, I’ve had no luck at all trying to get it to work. It won’t instantiate, populate cards, or return the correct type when drawing the card. I’ve tried mixing and matching and I just simply can’t get it to work.

Old Code that was buggy was truncated to save space, look at previous edits to see. Summary: I used Cardable instead of T and lacked to express generics in general.

So how would this work, I’m completely new to generics. I’ve been looking around everywhere and I keep hearing about Type Erasure and that the class literal should be a parameter and yadda yadda… But then how does ArrayList do it? Why is it that you can just type ArrayList<String>() and it will just work without needing something ridiculous like ArrayList<String>(String.GetClass())?

Thanks for your time.

Edit: Cardable is a class in which any card that can be put into the deck will extend.

Edit2: Perception’s suggestion has thus fixed my code, but I am not sure how I could call to populate the deck. Right now I have it to accept an array, but it would be nice to have it internal, and I’m not entirely sure I grasp the entire factory method.

public class Deck<T extends Cardable>
{
    private ArrayList<T> cardsInDeck;

    public Deck()
    {
        cardsInDeck = new ArrayList<T>();
    }

    public void populate( T[] newCards )
    {
        cardsInDeck.clear();
        for( T card : newCards )
        {
            cardsInDeck.add( card );
        }
        shuffle();
    }

    public T drawCard()
    {
        T card = null;
        try
        {
            card = cardsInDeck.get( 0 );
        }
        catch( IndexOutOfBoundsException e )
        {
            System.out.println( "Ran out of Cards" );
            e.printStackTrace();
        }
        cardsInDeck.remove( 0 );
        return card;
    }

    public void shuffle()
    {
        ArrayList<T> newDeck = new ArrayList<T>();
        Random rand = new Random();
        while( !cardsInDeck.isEmpty() )
        {
            int index = rand.nextInt( cardsInDeck.size() );
            newDeck.add( cardsInDeck.get( index ) );
            cardsInDeck.remove( index );
        }
        cardsInDeck = newDeck;
    }

    public int getSize()
    {
        return cardsInDeck.size();
    }
}
  • 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-17T13:11:27+00:00Added an answer on June 17, 2026 at 1:11 pm

    The thing is: the implementation of ArrayList<E> does not depend on the actual type E. That’s why you don’t need to pass the type in the constructor (as you say, new ArrayList<String>(String.class)).

    If you write a generic class that, for some reason, must know exactly what the generic type represents at runtime, then you need to pass the type in the constructor, because, as you said, type erasure will not allow you to get the class from T. You’d need new MyClassThatNeedToKnowItsActualTypeParameter<String>(String.class).

    For instance, suppose a class that accesses a database and retrieves an instance of a given class. The instances of a class are stored in a table named after the class. For example:

    class MyRepository<T> {
      T load(int id) { ... }
    }
    

    The method load needs to know exactly what T is at runtime, because it needs to be able to construct a query that will use the name of the actual class which T represents. However, in Java, you cannot obtain this information from T , since T will disappear due to type erasure. Furthermore, the load method needs a way to create an instance of the correct type and write data from the database to it. To create an instance of a class, you’d use reflection, doing clazz.newInstance() for example. Here, again, you need to know exactly what class you are dealing with. You’d end up with something like this:

    class MyRepository<T> 
      private final Class<T> clazz;
      MyRepository(Class<T> clazz) { this.clazz = clazz; }
      T load(int id) {
        final String tableName = clazz.getSimpleName() + "Table";
        /* connect, retrieve data, disconnect */
        final T t = clazz.newInstance(); // must be inside try/catch
        /* fill instance t with data from database somehow (using reflection probably, which, again, needs to know what clazz is */
        return t;
      }
    }
    
    ...
    final MyRepository<User> userRepository = new MyRepository<User>(User.class);
    final User user = userRepository.load(123);
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
This could be a duplicate question, but I have no idea what search terms

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.