constructor with an arraylist parameter?
I'm making a card game and I want to make a shuffle class to randomize the cards.
I'm using an arraylist to store all the cards and I was planning on using the arraylist as a parameter for my constructor, but netbeans won't let me do it.
I was hoping it'd look like
class.shuffle(arraylist);
And then I could use that in my main class to shuffle the cards.
Re: constructor with an arraylist parameter?
Consider making a Pack class to represent a pack of cards. (One class: one thing. Whatever sort of thing this Main class is, it is unlikely it is a pack of cards.)
Then give Pack a shuffle() method. If a Pack instance contains a list of Card instances then the pack's shuffle() could be as simple as calling Collections.shuffle() with that list.
In an instance of Main you could then do things like create an instance of Pack and shuffle() it, dealCard() etc as you wish.
Re: constructor with an arraylist parameter?
Thanks for the answer, but I tried it and it said "package does not exists <identifier required>
Quote:
public class shuffle {
LinkedList<String> deck= new LinkedList<String>();
deckadd("ace of Spades");
deck.add("two of Spades");
....
deck.add("king of Hearts");
}
Apparently I can only create an arraylist in the main class.
Re: constructor with an arraylist parameter?
Call the class what it is: Pack. The deck.add() stuff belongs in its constructor - what you posted won't compile. (Which might explain compiler's grumbling. Statements have to go inside a method or constructor.)
suffle() should be a method. And do look up the Collections.shuffle() method - it pretty much does what you're after.
Re: constructor with an arraylist parameter?
Thank you, that worked. :-bd
Re: constructor with an arraylist parameter?