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();
}
}
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 neednew 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:
The method
loadneeds 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 fromT, sinceTwill disappear due to type erasure. Furthermore, theloadmethod 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, doingclazz.newInstance()for example. Here, again, you need to know exactly what class you are dealing with. You’d end up with something like this: