user5903386
user5903386

Reputation: 57

How to move the first four elements of an arraylist into another arraylist?

This is my deck class:

public class Deck {    
    ArrayList<Card> deck = new ArrayList<Card>();    
    public ArrayList<Card> get32Cards() {    
        deck.add(new Card(Suit.SPADES, Rank.JACK));
        deck.add(new Card(Suit.SPADES, Rank.KING));
        deck.add(new Card(Suit.SPADES, Rank.SEVEN));
        deck.add(new Card(Suit.SPADES, Rank.EIGHT));
        deck.add(new Card(Suit.SPADES, Rank.NINE));
        deck.add(new Card(Suit.SPADES, Rank.TEN));
        deck.add(new Card(Suit.SPADES, Rank.ACE));
        deck.add(new Card(Suit.SPADES, Rank.QUEEN));
        deck.add(new Card(Suit.HEARTS, Rank.JACK));
        deck.add(new Card(Suit.HEARTS, Rank.KING));
        deck.add(new Card(Suit.HEARTS, Rank.SEVEN));
        deck.add(new Card(Suit.HEARTS, Rank.EIGHT));
        deck.add(new Card(Suit.HEARTS, Rank.NINE));
        deck.add(new Card(Suit.HEARTS, Rank.TEN));
        deck.add(new Card(Suit.HEARTS, Rank.ACE));
        deck.add(new Card(Suit.HEARTS, Rank.QUEEN));
        deck.add(new Card(Suit.CLUBS, Rank.JACK));
        deck.add(new Card(Suit.CLUBS, Rank.KING));
        deck.add(new Card(Suit.CLUBS, Rank.SEVEN));
        deck.add(new Card(Suit.CLUBS, Rank.EIGHT));
        deck.add(new Card(Suit.CLUBS, Rank.NINE));
        deck.add(new Card(Suit.CLUBS, Rank.TEN));
        deck.add(new Card(Suit.CLUBS, Rank.ACE));
        deck.add(new Card(Suit.CLUBS, Rank.QUEEN));
        deck.add(new Card(Suit.DIAMONDS, Rank.JACK));
        deck.add(new Card(Suit.DIAMONDS, Rank.KING));
        deck.add(new Card(Suit.DIAMONDS, Rank.SEVEN));
        deck.add(new Card(Suit.DIAMONDS, Rank.EIGHT));
        deck.add(new Card(Suit.DIAMONDS, Rank.NINE));
        deck.add(new Card(Suit.DIAMONDS, Rank.TEN));
        deck.add(new Card(Suit.DIAMONDS, Rank.ACE));
        deck.add(new Card(Suit.DIAMONDS, Rank.QUEEN));

        Collections.shuffle(deck); //this shuffles the deck

        return deck;
    }
}

and this is my main class:

public class Game {    
    public static void main(String[] args) {
        ArrayList<Card> deck = new ArrayList<Card>();

        Deck getdeck = new Deck();
        deck = getdeck.get32Cards();

        for (Card info : deck) {
            System.out.println(info);
        }  
    }
}

I have a piece of code that i think will work, but not sure how to implement it, or if it is even right:

 public boolean giveFourCards(Card c) {
        for (int x = 1; x < 3; x++) {
            if (deck.contains(c)) {
                deck.remove(c);
                Hand1.add(c);
                return true;
            } else
                return false;
        }
        return false;    
    }

I want to copy the first 4 elements of the 'deck' arraylist into another arraylist called 'hand1', and then remove those 4 elements from the 'deck' arraylist.

Upvotes: 1

Views: 94

Answers (3)

Jean-Baptiste Yun&#232;s
Jean-Baptiste Yun&#232;s

Reputation: 36391

Two problems:

  1. you are returning after the first iteration
  2. you are testing is a given card is in the deck?

This may correct the problems:

public giveFourCards(Hand h) {
  for (int x = 0; x < 3; x++) {
    h.add(deck.remove(0)); // push the removed card into the hand
  }
}

Upvotes: 0

Greesh Kumar
Greesh Kumar

Reputation: 1887

if you want get first 4 cards from deck then you can

public ArrayList<Card> giveFourCards() {
       ArrayList<Card> hand=new ArrayList<Card>();
       for (int x = 0; x < 3; x++) {
            hand.add(deck.get(x));  
       }
    return hand;

  }

Upvotes: 1

Xvolks
Xvolks

Reputation: 2155

Your giveFourCards() method receive a Card instance as a parameter : why ?

Your for loop seems wrong : should be for (int x=0; x<4; x++) {.

You seem to have copied block of code that you have not understood.

As I understand your question it should be:

public boolean giveFourCards() {
    for (int x=0; x<4; x++) {
        if (!deck.isEmpty()) {
            Hand1.add(deck.removeFirst());
        } else {
            return false;
        }
    }
    return true;
}

Upvotes: 1

Related Questions