Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: constructor with an arraylist parameter?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: constructor with an arraylist parameter?

    Thanks for the answer, but I tried it and it said "package does not exists <identifier required>
    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.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: constructor with an arraylist parameter?

    Thank you, that worked.

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: constructor with an arraylist parameter?

    You're welcome.

Similar Threads

  1. [SOLVED] sending a parameter to a new JDialog
    By luisp88 in forum AWT / Java Swing
    Replies: 4
    Last Post: October 5th, 2011, 09:31 AM
  2. [SOLVED] Send and Receive Parameter
    By java_mein in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: May 14th, 2010, 02:22 PM
  3. Initialization parameter of servlets
    By rakesh in forum Java Servlet
    Replies: 3
    Last Post: April 13th, 2010, 03:14 AM
  4. How do you pass an Array as a Parameter?
    By Arius in forum Java Theory & Questions
    Replies: 1
    Last Post: January 23rd, 2010, 09:36 PM
  5. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 04:31 AM