We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,703 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion
Page 2 of Article: Using ArrayList for first time
Hi again, I posted last week about a Go Fish game that I was trying to write - well I've sorted out my past issues and now I'm trying to rewrite my CardPile class (that defines such things as the deck or a player's hand) to consist of an ArrayList…

I just solved my problem - or I should say problems... It turns out I was just getting ahead of myself and made several stupid mistakes. Thank you for all your help!

sammoto
Light Poster
41 posts since May 2012
Reputation Points: 14
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 10 Months Ago by NormR1 and JamesCherrill

can you post the final code?

PraveenSri
Newbie Poster
1 post since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

how did you manage to solve your error?

curiousgeorgem
Newbie Poster
4 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

I just got cocky and thought I could rewrite a couple methods to be more "elegant" than the original design, but I ended up making a few small errors in the process. Here's the final code:

import java.util.Random;
import java.util.List;
import java.util.ArrayList;

public class CardPile {
    private ArrayList<Card> cards = new ArrayList<Card>();

    private static Random r = new Random(1);

    public void addToBottom(Card c) {
        if (this.cards.size() == 52) {
            System.out.println("The CardPile is full. You cannot add any more Card objects.");
        }
        this.cards.add(c);
    }

    public Card removeCard(Card c) {
        if (this.cards.contains(c)) {
            this.cards.remove(c);
        }
        return null;
    }

    public Card removeTop() {
        return this.cards.remove(0);
    }

    public int searchValue(int value) {
        int count = 0;
    //I know there is an ArrayList method that gives me the first/last
    //indices of a specified Object, but we're just looking for the value-
    //aspect of the Card, not the suit, so I decided to stick with the old logic
        //See below method for explanation of following line
        //System.out.println("2. cards=" + cards);
        for (int i = 0;i < this.cards.size();i++) {  
            if (this.cards.get(i).getValue() == value) {
                count++;
            }
        } 
        //System.out.println("Count = "+count);
        return count;
    }

    public Card[] removeAll(int value) {
        //I was having a lot of trouble with my removeAll and searchValue methods
        //but they turned out to be stupid mistakes… I left the following "print check"
        //line in anyway to show how I narrowed down my problem
        //System.out.println("(removeAll) cards ="+ cards);
        int count = searchValue(value);
        Card[] removed = new Card[count];
        int deletedCount = 0;
        int i = 0;
        while (deletedCount < count) {
            if (this.cards.get(i).getValue() == value) {
                removed[deletedCount] = this.cards.remove(i);
                deletedCount++;
            } else {
                i++;
            }
        }
        return removed;
    }

    public int getNumberCards() {
        return this.cards.size();
    }

    public String toString() {
        if (this.cards.isEmpty()) {
            return "";
        }
        String builder = "";
        for (int i = 0;i < this.cards.size() - 1;i++) {
            builder = builder + this.cards.get(i) + ", ";
        }
        builder = builder + this.cards.get(this.cards.size() - 1);
        return builder;
    }

    public void shuffle() {
        if (this.cards.isEmpty()) {
            return;
        }
        for (int count = 0; count < 100000;count++) {
            int i = r.nextInt(this.cards.size());
            int j = r.nextInt(this.cards.size());
            Card temp = this.cards.get(i);
            this.cards.set(i, this.cards.get(j));
            this.cards.set(j, temp);
        }
    }

    public static CardPile makeFullDeck() {
        CardPile deck = new CardPile();
        for (int suit = 0;suit < 4;suit++) {
            for (int value = 1; value <= 13;value++) {
                deck.addToBottom(new Card(suit, value));
            }
        }
        deck.shuffle();
        return deck;
    }
}
sammoto
Light Poster
41 posts since May 2012
Reputation Points: 14
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0739 seconds using 2.8MB