1

Move the first half of cards into firstHalf // This may or may not be done in one line of code Move the remaining elements in cards into secondHalf // This may or may not be done in one line of code While there are elements in firstHalf Remove the element at the start of firstHalf and add it to cards Remove the element at the start of secondHalf and add it to cards EndWhile If secondHalf has any elements remaining Remove the element at the start of secondHalf and add it to cards EndIf

this is the error I am getting. Why is my list growing if I am removing?Should I be doing something like this inside my first for loop firstHalf.add(cards.remove(0));
cards= firstHalf.add(cards.remove(0));

this is the error I am getting
shuffle() should not change the number of elements in cards. cards had contained a complete 52 card deck, but now has size: 71 expected:<52> but was:<71>

public void shuffle(ListGenerator gen) {


    List<PlayingCard> firstHalf = gen.createNewList();
    List<PlayingCard> secondHalf = gen.createNewList();
    for (int i = 0; i <= (cards.size() / 2); i++) {
     firstHalf.add(cards.remove(0));
    }
    secondHalf.addAll(cards);
    while (firstHalf.isEmpty() == false) {
        cards.add(firstHalf.remove(0));
        cards.add(secondHalf.remove(0));
    }
    if (secondHalf.isEmpty() == false) {
        cards.add(secondHalf.remove(0));
    }
}*
1
  • Whats ListGenerator ? Commented Oct 17, 2018 at 1:21

5 Answers 5

3

you need to clear the cards because cards still has elements which were previously added to secondHalf

secondHalf.addAll(cards);
cards.clear();
//^^^^^^^^^^^^
Sign up to request clarification or add additional context in comments.

5 Comments

this is the error I am getting now shuffle() should not change the number of elements in cards. cards had contained a complete 52 card deck, but now has size: 37 expected:<52> but was:<37>. should I be doing something like for (int i = (cards.size() / 2) ; i <= cards.size() ; i++) { secondHalf.add(cards.remove(0)); }
can you explain the error? (although you can use Collections.shuffle(cards);)
try int l = cards.size() / 2; for(int i = 0; i <= l ; i++) , avoid calculating the size at runtime in loop during every iteration
Is the firstHalf of the list cards (cards.size()/2) or is it (cards.size()-1/2)?
both are same 51/2 will give you 25 and as you are cards.remove(0) so you get first half in firstHalf
1

When you call secondHalf.addAll(cards);, you're not removing those cards from cards. Add this afterwards:

cards.clear();

2 Comments

for someone reason this is still changing the size of my list
this is the error i get shuffle() should not change the number of elements in cards. cards had contained 4 complete card decks for a total of 208 cards, but now has size: 141 expected:<208> but was:<141>
1

You are using the same variable i.e card in for loop and also removing the item from same card variable. Therefore, you are actually adding less number of element in firstHalf and your card still has the size that exceeds half of its original size.

 for (int i = 0; i <= (cards.size() / 2); i++) {
  firstHalf.add(cards.remove(0));
}

the size of firstHalf after the for loop ::18

the size of the card after the for loop::34

you should use the different variable for loop. like:

List<Integer> cardSize = cards.size();
  for (int i = 0; i <= (cardSize / 2); i++) {
  firstHalf.add(cards.remove(0));
}

I believe it should solve your issue.

Comments

0

I'd expect

secondHalf.addAll(cards);

Doesn't remove the entries from cards. Add a test after that line to see if cards is empty.

Comments

0
secondHalf.addAll(cards)

Here you are adding elements to the secondHalf, but you didn't remove the elements in cards. Add below line to code.

cards.clear();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.