Results 1 to 4 of 4
  1. #1
    ImAFycus is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default ArrayList duplicated/replaced objects

    alright, this seems to be a pretty simple problem if you know what's going on here, i just for some reason can't figure out a way around it.. i'm just not familiar with the convention of how to avoid it really...

    my issue is that i want to simulate a deck of cards but i'm having difficulty adding these objects without updating the ones already in the list. When ever I call new, what seems to happen is i always allocate the same object and just repeated add the same thing... how do i avoid this...

    here's my code:

    public class Card implements Comparable<Card>{

    private static int number;
    public enum Suit{hearts, spades, dimons, clubs};
    private static Suit suit;

    public Card (int rank, Suit suit){
    number = rank;
    Card.suit = suit;
    }

    int getRank(){return number;}
    Suit getSuit(){return suit;}

    public int compareTo(Card card){
    if(number>card.getRank()){return 1;}
    if(number<card.getRank()){return -1;}
    else{return 0;}
    }
    public String toString(){
    String val;
    if(number == 1){val = "A";}
    else if(number == 11){val = "J";}
    else if(number == 12){val = "Q";}
    else if(number == 13){val = "K";}
    else{val = Integer.toString(number);}
    return new String(val + " " + suit);
    }
    }


    import java.util.ArrayList;

    public class Deck {

    private ArrayList<Card> deck;

    public Deck()
    {
    deck = new ArrayList<Card>();
    for ( Card.Suit suit : Card.Suit.values()){
    for ( int rank = 1; rank <= 13;rank++ ){
    deck.add(new Card(rank,suit));
    }
    }
    }

    public boolean print(){
    for(Card c: deck){
    System.out.println(c);
    }
    return false;
    }

    public static void main(String[] args){
    new Deck().print();
    }
    }

    the console prints K clubs 52 times which is obviously not the desire effect... what is wrong? What should I be doing instead?

    Thanks.

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Java Code:
    public class Card implements Comparable<Card>{
    
      private [color="red"][b]static[/b][/color] int number;
      public enum Suit{hearts, spades, dimons, clubs};
      private [color="red"][b]static[/b][/color] Suit suit;

    Your problem is likely due to your use of static fields which are not fields of the object but rather are fields of the class. Thus if you change the contents of a static field in one object, you effectively change it for all objects since again, it is not a field that is tied to that particular object. Get rid of all statics in your code except for that which is needed for the main method, and then please let us know how things are working.
    Last edited by curmudgeon; 05-29-2010 at 08:51 PM.

  3. #3
    ImAFycus is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Genius! Thank you!
    :D

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by ImAFycus View Post
    Genius! Thank you!
    :D
    More like just another blind fool who has stumbled over this branch often enough to be able to warn you about it. You're welcome.

Similar Threads

  1. Replies: 9
    Last Post: 02-10-2010, 04:19 AM
  2. How can i store ArrayList objects in Access database
    By frankycool in forum Advanced Java
    Replies: 3
    Last Post: 11-04-2009, 07:55 AM
  3. Retrieving specific objects from ArrayList
    By soketti in forum New To Java
    Replies: 49
    Last Post: 10-29-2009, 08:10 AM
  4. Reset form button and duplicated data
    By dbashby in forum New To Java
    Replies: 7
    Last Post: 10-10-2009, 05:40 AM
  5. ArrayList with different objects? Help
    By xtrmi in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 09:51 PM

Tags for this Thread

Posting Permissions

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