Results 1 to 5 of 5
  1. #1
    dstars5 is offline Member
    Join Date
    Mar 2013
    Posts
    19
    Rep Power
    0

    Default ArrayList randomly changes after for loop

    Hey guys,

    So I am writing a Blackjack program, and I've created a class to store the deck of cards. When I created a new Deck, the program should create new Card classes with a name(taken from an Array) and the value of the card. This is added to an ArrayList that holds the deck. This might make more sense once you see the code, but basically, I print out the name and value of each card before and after the end of the for loop. Before, it works fine. After, however, every single card is a King of value 10 (Only thing I can figure out is that this is because King is the last String in the array. If I remove King, Queen becomes the last and everything is turned into a queen.

    Here is the public Deck() that is not working correctly:

    Java Code:
    public Deck(int d) {
    		String names[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    		
    		cardsback.add(new Card("TERETRERR", 100));
    		System.out.println(cardsback.get(0));
    		
    		for (int i = 0; i < d; i++) {
    			for (int j = 0; j < names.length; j++) {
    				int val = j + 1;
    				
    				if (val > 10) {
    					val = 10;
    				}
    				
    				for (int k = 0; k < 4; k++) {
    					cardsback.add(new Card(names[j], val));
    					System.out.print(cardsback.size() - 1 + " ");
    					System.out.println(cardsback.get(cardsback.size() - 1)); //This prints out properly.
    				}
    				
    				System.out.println("---------------------------- " + cardsback.get(cardsback.size() - 1)); //This prints out properly.
    			} 
    		}
    		
    		for (int i = 0; i < cardsback.size(); i++) {
    			System.out.print(i + " ");
    			System.out.println(cardsback.get(i)); //This is where it messes up and everything prints as a King.  Even the "TERETRERR" card I added at the top.
    		}
    		
    		shuffle();
    	}
    And the full class:

    Java Code:
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    
    public class Deck {
    	private static List<Card> cards = new ArrayList<Card>();
    	private static List<Card> cardsback = new ArrayList<Card>();
    	
    	public Deck() {
    		String names[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    		
    		for (int i = 0; i < names.length; i++) {
    			int val = i + 1;
    			
    			if (val > 10) 
    				val = 10;
    			
    			
    			Card c = new Card(names[i], val);
    			for (int k = 0; k < 4; k++)
    				cardsback.add(c); 
    		}
    		
    		shuffle();
    	}
    	
    	public Deck(int d) {
    		String names[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    		
    		cardsback.add(new Card("TERETRERR", 100));
    		System.out.println(cardsback.get(0));
    		
    		for (int i = 0; i < d; i++) {
    			for (int j = 0; j < names.length; j++) {
    				int val = j + 1;
    				
    				if (val > 10) {
    					val = 10;
    				}
    				
    				for (int k = 0; k < 4; k++) {
    					cardsback.add(new Card(names[j], val));
    					System.out.print(cardsback.size() - 1 + " ");
    					System.out.println(cardsback.get(cardsback.size() - 1));
    				}
    				
    				System.out.println("---------------------------- " + cardsback.get(cardsback.size() - 1));
    			}
    		}
    		
    		for (int i = 0; i < cardsback.size(); i++) {
    			System.out.print(i + " ");
    			System.out.println(cardsback.get(i));
    		}
    		
    		shuffle();
    	}
    	
    	public static List<Card> getDeck() {
    		return cards;
    	}
    	
    	public static void shuffle() {
    		cards = cardsback;
    		
    		Collections.shuffle(cards);
    	}
    	
    	public static Hand draw(int num) {
    		Hand h = new Hand();
    		
    		for (int i = 0; i < num; i++) {
    			h.addCard(cards.get(0));
    			cards.remove(0);
    		}
    		
    		return h;
    	}
    
    	public String toString() {
    		String s = "";
    		
    		for (int i = 0; i < cards.size(); i++) {
    			s = s + " --- " + cards.get(i);
    		}
    		
    		return s;
    		
    	}
    }
    Does anyone know why this is happening?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: ArrayList randomly changes after for loop

    It would probably help if you would show the complete program. For example, the Card class is missing. However, you realize that because your cards and cardsback variables are static, they are considered class fields. So they are not unique between instances of Deck.

    And if you do the following:

    cards = cardsback;

    You are shuffling both because the both refer to the same collection.

    Regards,
    Jim

    Collections.shuffle(cards);
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    dstars5 is offline Member
    Join Date
    Mar 2013
    Posts
    19
    Rep Power
    0

    Default Re: ArrayList randomly changes after for loop

    Ah, wow, I feel dumb now haha. I've never used multiple classes before, so I never even thought about the static issue. It ended up being that value and name were both static in the Card class. Thank You!

    As for setting the cards variable to the cardsback variable, should I be using cards.addAll(cardsback) instead? Will that keep them seperate?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: ArrayList randomly changes after for loop

    Quote Originally Posted by dstars5 View Post
    As for setting the cards variable to the cardsback variable, should I be using cards.addAll(cardsback) instead? Will that keep them seperate?
    Yes! That should work. As long as cards is a new instance of the class Cards or you clear the contents first. Otherwise, the cards instance will keep
    growing. You could also simply pass one list of cards to the constructor when creating a new ArrayList.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    dstars5 is offline Member
    Join Date
    Mar 2013
    Posts
    19
    Rep Power
    0

    Default Re: ArrayList randomly changes after for loop

    Quote Originally Posted by jim829 View Post
    Yes! That should work. As long as cards is a new instance of the class Cards or you clear the contents first. Otherwise, the cards instance will keep
    growing. You could also simply pass one list of cards to the constructor when creating a new ArrayList.

    Regards,
    Jim
    Thank you!

Similar Threads

  1. for in while loop and arraylist advanced for
    By nonybrighto in forum New To Java
    Replies: 4
    Last Post: 11-20-2012, 03:26 PM
  2. arrayList in a while loop
    By Lund01 in forum New To Java
    Replies: 3
    Last Post: 11-06-2011, 11:28 PM
  3. How to run a for-each loop through an ArrayList?
    By simon.black325 in forum New To Java
    Replies: 6
    Last Post: 09-08-2011, 04:18 AM
  4. showing results in a for loop randomly
    By vexity in forum New To Java
    Replies: 4
    Last Post: 04-29-2008, 04:24 AM
  5. Iterating through ArrayList using For loop
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:53 AM

Posting Permissions

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