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: doel.jangkrik is an unknown quantity at this point 
Solved Threads: 0
doel.jangkrik doel.jangkrik is offline Offline
Newbie Poster

How do I count numbers inside LinkedList?

 
0
  #1
Jun 21st, 2007
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
  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
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: How do I count numbers inside LinkedList?

 
0
  #2
Jun 21st, 2007
tried to just run the list trough a loop and add the value of every element to a seperate counter?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,490
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How do I count numbers inside LinkedList?

 
0
  #3
Jun 21st, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 9
Reputation: doel.jangkrik is an unknown quantity at this point 
Solved Threads: 0
doel.jangkrik doel.jangkrik is offline Offline
Newbie Poster

Re: How do I count numbers inside LinkedList?

 
0
  #4
Jun 21st, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,490
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How do I count numbers inside LinkedList?

 
0
  #5
Jun 21st, 2007
Originally Posted by doel.jangkrik View Post
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).
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;
}
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC