944,161 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 4251
  • Java RSS
Jun 21st, 2007
0

How do I count numbers inside LinkedList?

Expand Post »
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
Java Syntax (Toggle Plain Text)
  1. 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
doel.jangkrik is offline Offline
9 posts
since Apr 2007
Jun 21st, 2007
0

Re: How do I count numbers inside LinkedList?

tried to just run the list trough a loop and add the value of every element to a seperate counter?
Reputation Points: 938
Solved Threads: 357
Posting Maven
stultuske is offline Offline
2,528 posts
since Jan 2007
Jun 21st, 2007
0

Re: How do I count numbers inside LinkedList?

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jun 21st, 2007
0

Re: How do I count numbers inside LinkedList?

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
doel.jangkrik is offline Offline
9 posts
since Apr 2007
Jun 21st, 2007
0

Re: How do I count numbers inside LinkedList?

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:
public static int countCards(LinkedList<Card> content){
int total = 0;
for (Card card : content){
  total += card.rank();
}
 
return total;
}
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is solved

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.
Message:
Previous Thread in Java Forum Timeline: Tomcat Server
Next Thread in Java Forum Timeline: A few questions regarding video, images and audio





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC