• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about for loop

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works:
int tempHandSize = tempHand.cardGroupSize();
for(int k=0; k<tempHandSize; k++)
{addCard(tempHand.removeCard(0));}
This Doesnt:
for(int k=0; k<tempHand.cardGroupSize(); k++)
{addCard(tempHand.removeCard(0));}
...
public int cardGroupSize()
{
return cardGroup.size();
}
does a for loop check the run the method each time?..
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joshua,


does a for loop check the run the method each time?..


Yup.
Michael Morris
SCJP2
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you want your loop to remove all elements from your collection, it would be better to remove element k instead of element 0. That way you'll never have any problems.
In terms of performance the first solution is better, though, otherwise at each iteration a new activation frame will be created just to retrieve the size of your collection.
[ March 28, 2002: Message edited by: Valentin Crettaz ]
 
Joshua Kueck
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually.. I'm removing the 0 element because if I used k, I wouldn't get them all. The Vector moves all elements down when one is removed, Id be skipping elements! Figured this out the hard way
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then you could do it from the end like this:

Or you could use the clear() or removeAll() methods, but I think you actually have to get a reference on the object your are removing, right?
 
Joshua Kueck
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
..That would work .. but it would reverse the order. Of course, the order doesn't matter too much with what I'm doing. What I'm doing is sorting cards in sortRankAndSuit(), the method this code is in. In the method, I first call sortSuit(), to sort all the cards into groups of suits. Then I pull out each suit group into tempHand, call tempHand.sortRank(), and put tempHand back into the main Hand vector. So when all suits are done, the cards are sorted 2C 4C 6C 3S 5S.. etc It works pretty good. Probably could be more efficient, but hands don't really get too big
 
Joshua Kueck
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the errors aren't in place yet..but you get the idea
public Card removeCard(int i)
{
Card card=new Card("damn2","damn2");
if (cardGroup.isEmpty() != true)
{
try {card=(Card)cardGroup.get(i); //get a reference to the card
cardGroup.removeElementAt(i); //remove from the CardGroup
cardGroup.trimToSize(); //get rid of any unused elements in vector, just incase
return card; //return the card
}
catch(ArrayIndexOutOfBoundsException e) { return new Card("damn3", "damn3");}

}
else { return card;}
}
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok sounds good.
If you allow me to nitpick your code,
if (cardGroup.isEmpty() != true)
should not be written that way. The condition you wrote makes it difficult to figure out when it is true actually. As a rule of thumb, you should not write "!= true", instead write "== false". And since cardGroup.isEmpty() already yields a boolean, you can just simply write (!cardGroup.isEmpty()) meaning "cardGroup is not empty" instead of "card group is empty is not true". Just thought you'd like to know...
 
Joshua Kueck
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, Ok. Thanks .. I originally had it like that, but I had a bug somewhere so I went through and made everything as explicit as possible. Im actually up to a table now.. as in.. table has dealer, players, and a pot(some of them ).. it handles communication between all the players and what not (whats face up, total in pot, etc) dealer has a deck, players has a hand, a discard,.. deck is a cardgroup, hand is a cardgroup, pile is a cardgroup, etc.... cardgroup has a Vector of cards... all that and I forgot one thing.. face up or face down .. I guess a card knows if it is face up or face down.. so Ill add that to card and some methods to access it.. My main problem is computer players, I think the best way is for a table to have a Vector of both players and computer players, that way I can check to see if its a computer player, then call its hitMe(), if not.. do something to get players input..
I forgot how much fun programming is.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic