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 8 of 8

Thread: ArrayList confusion

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Unhappy ArrayList confusion

    Basically, I am creating a CardGuessing game (A user tries to guess a card by choosing whether to guess the suit,value or card, the target card changes for each game and the details of the 52 cards are stored in an ArrayList) I am very confused as to why my teacher wants me to use ArrayList<Card> to store details of all 52 cards because the size is always going to be 52... so wouldn't an array be more sufficient?

    I created an ArrayList of strings containing all 52 cards but got stuck on how I could use it with this method i've been given.

    This method is in my Pack class; public Card getRandomCard(), which should return a random card from the ArrayList also in the Pack class.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: ArrayList confusion

    The ArrayList implementation is built off of Arrays. Basically anywhere you can use Arrays it can be replaced by an ArrayList. While the other way around is also true*, it can (and usually does) require more work on your part to implement correctly (particularly when it requires re-sizing the array, or removing items in the middle of the array).

    *note: there is one major difference in that ArrayLists can be used with objects that have generic parameters, where-as you can never have an array of objects which have generic parameters. To get something like this, you will need to implement something like the ArrayList implementation, a rather moot point because it's already been implemented by someone else.

    To get a random number in Java:

    // this will generate a random number [0,52)
    int number = (int)(Math.random() * 52.0);

    This is just one of the ways, there are two easy ways through the standard API, or you can implement your own random number generator which is actually quite difficult to do well.
    Last edited by helloworld922; August 13th, 2010 at 01:24 PM.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Stormin (August 13th, 2010)

  4. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList confusion

    import java.util.Random;
    import java.util.ArrayList;
     
     
    /**
     * Stores the details of all 52 cards in an ArrayList.
     * A card is selected from random from the ArrayList for the current game.
     * 
     */
    public class Pack
    {
        private ArrayList<Card> cards;
        private Random RandomGenerator;
     
        /**
         * ArrayList containing 52 cards.
         */
        public static void main(String[] args) {
     
             ArrayList<Card> cards = new ArrayList<Card>();
     
            Card.add("C1");
            Card.add("C2");

    Not showing all the elements in the arrayList but I get the error, any advice?
    cannot find symbol - method add(java.lang.String) on thefirst array in the list.

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: ArrayList confusion

    The add method assumes you are trying to send it the object of the ArrayList.

    So, since your ArrayList is Card Objects, which you declared by saying ArrayList<Card> cards = new ArrayList<Card>();, you have to send it Card Objects with the add method.

    You are attempting to send it a String.

    So, instead of
    Card.add("C1");
    Card.add("C2");

    Say:
    cards.add(new Card("C1"));
    cards.add(new Card("C2"));

    I am assuming you send your card object a String when you construct it.

  6. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList confusion

    Using the 'add' method now gives me this error:
    cannot find symbol - constructor Card(java.lang.String)

    My Card object consists of 2 char attributes; value & suit. So I would need the array list to contain two chars
    instead of strings right? I'm guessing that's the problem, if it's possible to do so.

    Sorry I'm very new to java and i'm reading all I can but some parts I don't understand fully.

  7. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: ArrayList confusion

    No.

    You need to construct a Card Object and send that to the ArrayList with the add method.

    When you said:
    ArrayList<Card>
    You told the ArrayList that it will hold ONLY Card Objects. So, the only thing we can send the ArrayList is Card Object.

    So, create your Card Object by saying something like:
    Card c = new Card('9','H');

    Then send your Card Object to the ArrayList by saying:
    cards.add(c);

    Put together, it looks like this:
    Card c = new Card('9','H');
    cards.add(c);

    One of the things you can do in JAVA (which I did earlier but I think it went over your head) is creating Objects on the fly with intention of never referring to that Object by a static variable.

    So when I write:
    cards.add(new Card('9','H'));

    It is just a short-hand way of writing:
    Card c = new Card('9','H');
    cards.add(c);

  8. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Stormin (August 13th, 2010)

  9. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: ArrayList confusion

    The generic parameter does not allow you to add strings.

    ArrayList<[b]Card[/b]> cards; // this can only hold objects of the type Card

    You must construct a card object based off of the string in order to add that card to the ArrayList.

    // a sample card constructor
    // your constructor will probably have a similar signature, but the code inside will differ
    public Card(String representation)
    {
        this.representation = representation;
    }

  10. The Following User Says Thank You to helloworld922 For This Useful Post:

    Stormin (August 13th, 2010)

  11. #8
    Junior Member
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList confusion

    Thanks for the replies guys. much appreciated.

    Yeah I have my Card Constructor in my Card Class.
    public class Card
    {
        private char suit, value; 
        private String SuitName; 
        private String ValueName; 
     
        /**
         * Constructor for objects of class Card
         */
      Card(char suit, char value)
        {
             this.suit = suit;
             this.value = value;
         }
    And my ArrayList in my Pack class
    public class Pack
    {
        private ArrayList<Card> cards;
        private Card RandomCard;
     
        /**
         * ArrayList containing 52 cards.
         */
        public static void main(String[] args) {
     
             ArrayList<Card> cards = new ArrayList<Card>();
     
     
            cards.add(new Card('C','A'));

Similar Threads

  1. Confusion about DAO in Three-Tier Architecture!
    By maven in forum Object Oriented Programming
    Replies: 1
    Last Post: July 24th, 2010, 08:09 AM
  2. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  3. Magic Squares, input confusion
    By bengiles89 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 08:40 PM
  4. Scanner class error "java.lang.Error"
    By Lheviathan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2009, 02:23 AM
  5. Confusion in creating class with OOPS concept
    By grbsmj in forum Object Oriented Programming
    Replies: 3
    Last Post: May 6th, 2009, 03:14 AM