Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2011
    Location
    NY
    Posts
    5
    Rep Power
    0

    Default Creating a list with objects

    Java Code:
    class DeckOfCards{
    
    	private List cards;
    	
    	public DeckOfCards(){
    		Card ace_Spades = new Card("Spades","Ace",11);
    		Card two_Spades = new Card("Spades","Two",2);
    		Card three_Spades = new Card("Spades","three",3);
    		Card four_Spades = new Card("Spades","four",4);
    		Card five_Spades = new Card("Spades","five",5);
    		Card six_Spades = new Card("Spades","six",6);
    		Card seven_Spades = new Card("Spades","seven",7);
    		Card eight_Spades = new Card("Spades","eight",8);
    		Card nine_Spades = new Card("Spades","nine",9);
    		Card ten_Spades = new Card("Spades","ten",10);
    		Card jack_Spades = new Card("Spades","jack",10);
    		Card queen_Spades = new Card("Spades","queen",10);
    		Card king_Spades = new Card("Spades","king",10);
    	
    	
    	}
    	
    	List cards = new List();
    	
    	cards.add(ace_Spades);
    
    }

    I am trying to add each object or "element" to a list. Using the java list class. The goal is to add each to a list, and then shuffle the list latter on in a method. I am making a card game..

    How would I go about adding each element to a list? To build my

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The add method is how you add to a list, I suggest you have the constrctor make an instance of the list. Then loop 52 times adding a new card each time. It may be helpful to have a card constructor which takes a single int as an argument and assigns the proper rank and suit based on the number(this can be done with % and /)

  3. #3
    Join Date
    Feb 2011
    Location
    NY
    Posts
    5
    Rep Power
    0

    Default

    class Card{

    private String suite;
    private String type;
    private int value;

    public Card(String cardSuite, String cardType, int cardValue){
    cardSuite = suite;
    cardType = type;
    cardValue = value;
    }
    }
    That is my card class.. Can you give me an example of the add method? I am not familiar with the List class

  4. #4
    Join Date
    Feb 2011
    Location
    NY
    Posts
    5
    Rep Power
    0

    Default

    I'm basically trying to add each card that has been instantiated into an List. I guess the correct term would be to add those to an Array Element List..

    I tried

    ArrayList cardList = new ArrayList();

    cardList.add(ace_Spades);

    not working

    DeckOfCards.java:67: <identifier> expected
    cardList.add(ace_Spades);
    Last edited by thine_iniquity; 03-25-2011 at 04:02 AM. Reason: oop

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    Executable code, such as cardList.add(...) has to be inside a method, constructor or initializer block. It can't be executed in the class body, outside of any of the above.

    db

  6. #6
    Join Date
    Feb 2011
    Location
    NY
    Posts
    5
    Rep Power
    0

    Default

    DUH! Thank you DB. It compiled successfully but gave me a note:

    Note: DeckOfCards.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.

    What is the meaning of this?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    This means you didn't use generics. Note: it's a warning, not an error. Technically it can run, but should be done better. Here is an example of employees and a compony to demonstrate arrayList.

    Java Code:
    class Employee{
      private int id;
      private int salary;
      public Employee(int id){
        this.id = id;
        this.salary = id * 10;
      }
    }
    public class Company{
      private ArrayList<Employee> employees = new ArrayList<Employee>(); //note the generics in the <>, it tells the list that it contains the type inside the <>
      public Company(int size){
        for(int i = 0; i < size; i++){
          Employee e = new Employee(i + 1);
          employees.add(e);
        }
      }
    }
    This code doesn't do much that's useful and is really just the beginning of a classes design. However, it shows how to add items to a list, and it shows generics as well.

    It would be really helpful to have another constructor in the card class that will take a single number and create each card like this
    Java Code:
    public class Deck{
      //declare arraylist
      public Deck(){
        for(int i = 0, i < 52; i++){
          Card c = new Card(i + 1);
          ArrayList.add(c);
        }
      }
    }

Similar Threads

  1. Need Help in Removing Objects from list
    By pramod_r20 in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 01:19 PM
  2. add objects to list
    By javaMike in forum Advanced Java
    Replies: 1
    Last Post: 11-17-2009, 11:11 PM
  3. List of objects
    By bubbless in forum New To Java
    Replies: 0
    Last Post: 08-01-2009, 05:21 PM
  4. List of objects
    By rekha in forum New To Java
    Replies: 6
    Last Post: 03-20-2009, 12:39 PM
  5. Getting objects from a list
    By markyoung1984 in forum New To Java
    Replies: 4
    Last Post: 03-13-2008, 11:45 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
  •