Results 1 to 3 of 3
  1. #1
    melbourn1te is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Simple ArrayList Q

    Hi,

    I hope this is an easy question to answer for you.

    I am making a card game that has a Card class and a Deck class. I am using an ArrayList to represent the cards in the deck, but I'm unsure on one particular part of the code.

    Java Code:
    public class Deck {
    
    	private ArrayList<Card> deck;
    	private Card[] initialDeck; 
    
    	/**
    	 * Create a new deck from an initial set of cards.
    	 * 
    	 */
    	public Deck(Card[] initialDeck)  {
    		super();
    		this.initialDeck = initialDeck;
    		deck = new ArrayList<Card>();
    This is what I have so far, what is confusing me is how initialDeck is a parameter in the constructor. Is this meant to be used in the ArrayList and if so, how do I do this?

    Thanks in advance if you can help!

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Sorry, but your question doesn't make any sense. Yes initialDeck is a parameter, is what meant to be used in the ArrayList?

    Card[] is an Array of Card
    deck is an ArrayList of Card

    Are you intentionally using a mix of both?

    to add an Array to ArrayList use java.util.Arrays.asList e.g.
    Java Code:
    Card[] initialDeck = new Card[52];
    java.util.List<Card> cardList = new java.util.ArrayList<Card>();
    cardList.addAll(java.util.Arrays.asList(initialDeck));

  3. #3
    melbourn1te is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Thank you so much for the help.

    The mix is intentional as far as I am aware, we were given a stubbed implementation to expand on, so the Arrays.asList worked perfectly.

    Thank you again!

Similar Threads

  1. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 07:07 AM
  2. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 12:48 PM
  3. Replies: 7
    Last Post: 11-22-2010, 02:26 PM
  4. Replies: 2
    Last Post: 11-16-2010, 06:51 PM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •