•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 402,384 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,994 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1109 | Replies: 4 | Solved
![]() |
•
•
Join Date: Apr 2007
Location: New Zealand
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
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.
Any help is appreciated.
doel
- 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
gambler.addFirst(myApp.cards.removeFirst());
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.
Any help is appreciated.
doel
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 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.
•
•
Join Date: Apr 2007
Location: New Zealand
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
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......
Thanks again for all input.
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.
•
•
•
•
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; }
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:
public static int countCards(LinkedList<Card> content){ int total = 0; for (Card card : content){ total += card.rank(); } return total; }
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- review my link directory (Website Reviews)
- Count number of one's in 32 bits (C)
- Calling Object Methods within a LinkedList (Java)
- How many different numbers are there? (C++)
- help with program (C++)
Other Threads in the Java Forum
- Previous Thread: Tomcat Server
- Next Thread: A few questions regarding video, images and audio



Linear Mode