I am trying to make text based blackjack game. I already made :
- Card class which create new card based on integer
- Deck class which consists of Deck LinkedList populated by Card.
I also made 2 new LinkedLists for gambler and dealer. I already managed to remove elements from a shuffled Deck and add them to the new Lists using
However I am stumped on how to count the total of the elements inside gambler and dealer without removing them. If I used peek() method, it only take the head of the lists.
If you are using the Java Collections LinkedList class, you can use the size() method to determine the number of elements in the list.
I am wondering though, why not use a different collection to hold the cards? Would a Vector of ArrayList work just as well? Is there a compelling reason you are using LinkedList? You should examine your usage of a collection of objects and choose the collection implementation based upon your needs.
I an a newbie at Java so I don't know much about Vector of ArrayList. Anyway I solved it by creating a temp LinkedList. I don't know if it's the most effective solution......
public static int countCards(LinkedList<Card> content){int total = 0;
LinkedList<Card> temp = new LinkedList<Card>();
temp = content;
while (!temp.isEmpty()){
total += temp.removeFirst().rank();
}
return total;
}
I an a newbie at Java so I don't know much about Vector of ArrayList.
Ah, sorry that was a typo that shoud have read Vector or ArrayList. They are merely array-backed collections of objects that provide for iteration and also random access (insertion, removal, retrieval).
Quote ...
Anyway I solved it by creating a temp LinkedList. I don't know if it's the most effective solution......
public static int countCards(LinkedList<Card> content){int total = 0;
LinkedList<Card> temp = new LinkedList<Card>();
temp = content;
while (!temp.isEmpty()){
total += temp.removeFirst().rank();
}
return total;
}
Thanks again for all input.
You don't really need to remove from the list just to examine things in the list. You can simply iterate the collection with a "for each" loop, so your example code becomes:
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Previous Thread in Java Forum Timeline:Tomcat Server