wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about for loop" Watch "about for loop" New topic
Author

about for loop

Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
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?..
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Joshua,

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

Yup.
Michael Morris
SCJP2


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
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
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Mar 14, 2002
Posts: 71
..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

Joined: Mar 14, 2002
Posts: 71
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
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Mar 14, 2002
Posts: 71
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about for loop
 
Similar Threads
breaking from a loop
one more doubt regarding operators
for loop and arrays
for loop prob - please HELP!!
While nested in do-while loop