java - Adding my items to an ArrayList -
i have created 52 cards added arraylist problem when try first item in list can outofboundserror because don't think has been added arraylist.
private arraylist<card> deck; public deck() { deck = new arraylist<card>(); for(int = 1; < 14; i++) { if(i == 1){ card c = new card("ace", "hearts"); } else if(i < 11 && > 1) { card c = new card("" + i, "hearts"); } else if(i == 11) { card c = new card("jack", "hearts"); } else if(i == 11) { card c = new card("queen", "hearts"); } else if(i == 11) { card c = new card("king", "hearts"); } } for(int = 1; < 14; i++) { if(i == 1) { card c = new card("ace", "clubs"); } else if(i < 11 && > 1) { card c = new card("" + i, "hearts"); } else if(i == 11) { card c = new card("jack", "clubs"); } else if(i == 11) { card c = new card("queen", "clubs"); } else if(i == 11) { card c = new card("king", "clubs"); } } for(int = 14; < 1; i--) { if(i == 11) { card c = new card("king", "diamonds"); } else if(i == 11) { card c = new card("queen", "diamonds"); } else if(i == 11) { card c = new card("jack", "diamonds"); } else if(i < 11 && > 1) { card c = new card("" + i, "diamonds"); } else if(i == 1) { card c = new card("ace", "diamonds"); } } for(int = 14; < 1; i--) { if(i == 11) { card c = new card("king", "spades"); } else if(i == 11) { card c = new card("queen", "spades"); } else if(i == 11) { card c = new card("jack", "spades"); } else if(i < 11 && > 1) { card c = new card("" + i, "spades"); } else if(i == 1) { card c = new card("ace", "spades"); } } }
how go adding arraylist?
to add cards deck need call
deck.add(c);
btw pointless using loop , checking 1 value in loop. can call line of code once.
list<card> deck = new arraylist<>(); (string suit : "hearts,clubs,diamonds,spades".split(",")) { deck.add(new card("ace", suit)); (int = 2; <= 10; i++) deck.add(new card("" + i, suit)); deck.add(new card("jack", suit)); deck.add(new card("queen", suit)); deck.add(new card("king", suit)); }
Comments
Post a Comment