Get Method
I wish to invoke a method to override the ArrayList.get() method. I wish to return the top card within a randomly shuffled pack of cards. This is ok if i use an arraylist and then arrayList.get(0), but i have created a Deck class which uses a deck ArrayList to hold cards.
If i invoke my own constructor:
Deck deck1 = new Deck();
im obviously not able to use the ArrayList.get() method, so how could i go about creating my own.
Any helpful pointers greatly appreciated. Thanks in advance.
[519 byte] By [
Royboy50] at [2007-9-30 23:00:41]

Fairly simply, though you might consider using a Stack instead of a List, as stacks are designed to return the top element on the stack. Really, when playing cards, you do deal from a 'stack' (aka deck) of cards.
final public class Deck {
final private List cards;
public Deck() {
super();
sortDeck();
}
final public boolean hasNextCard() {
return (cards.size() > 0);
}
final public String getNextCard() {
return (String) cards.get(0);
}
private void sortDeck() {
// place own code here
}
}
Rather than overriding ArrayList, it is *much* better to simply declare one in your class and then expose a public method with the functionality you want. This is called composition (and delegation) and should be used liberally.
- Saish
"My karma ran over your dogma." - Anon
Saish at 2007-7-7 13:34:25 >
