| | |
How do I count numbers inside LinkedList?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 9
Reputation:
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
Java Syntax (Toggle Plain Text)
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
Posts: 9
Reputation:
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......
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; }
public static int countCards(LinkedList<Card> content){ int total = 0; for (Card card : content){ total += card.rank(); } return total; }
![]() |
Similar Threads
- 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
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation binary blackberry block bluetooth character chat class classes client code component consumer database desktop developmenthelp draw eclipse error event exception fractal ftp game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaee javaprojects jmf jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner screen server set singleton size sms sort sql string swing template textfields threads time title tree tutorial-sample update windows working






