User Name Password Register
DaniWeb IT Discussion Community
All
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,735 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,471 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: 1113 | Replies: 4 | Solved
Reply
Join Date: Apr 2007
Location: New Zealand
Posts: 7
Reputation: doel.jangkrik is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
doel.jangkrik doel.jangkrik is offline Offline
Newbie Poster

Help How do I count numbers inside LinkedList?

  #1  
Jun 20th, 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
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2007
Posts: 139
Reputation: stultuske is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 16
stultuske's Avatar
stultuske stultuske is offline Offline
Junior Poster

Re: How do I count numbers inside LinkedList?

  #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  
Join Date: May 2007
Location: USA
Posts: 2,660
Reputation: Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light 
Rep Power: 11
Solved Threads: 262
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: How do I count numbers inside LinkedList?

  #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  
Join Date: Apr 2007
Location: New Zealand
Posts: 7
Reputation: doel.jangkrik is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
doel.jangkrik doel.jangkrik is offline Offline
Newbie Poster

Re: How do I count numbers inside LinkedList?

  #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  
Join Date: May 2007
Location: USA
Posts: 2,660
Reputation: Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light 
Rep Power: 11
Solved Threads: 262
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: How do I count numbers inside LinkedList?

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 7:29 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC