Results 1 to 2 of 2
- 06-30-2012, 03:26 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 5
- Rep Power
- 0
Enhanced For Loop Array List Reference Question
Im trying to make a texas hold em simulation app and have a question about enhanced for loop.
Im trying to come up with a methodto locate a certain card in the deck. I constructed an ArrayList of Card objects each having two integer instance variables (Rank and Suit).
Now in removing cards and adding cards back to the deck through the various deck methods i need a way to locate a certain Card Objects index in the array list deck.
Im thinking i can use the enhanced for loop like so:
now from here im thinking in the if conditional statement body i should putJava Code:public int findCard(int a, int b) { rankIndex = a; suitIndex = b; search: for(Card testCard : deck) { int testRank = testCard.getRank(); int testSuit = testCard.getSuit(); if( (testRank == rankIndex) && (testSuit == suitIndex)) {
not sure if thats correct or if im making correct use of "break", or if break is needed since i declare return.Java Code:int findCardIndex = deck.get(testCard); return findCardIndex; break search;
im very new to java this site has been great so far
- 06-30-2012, 03:42 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: Enhanced For Loop Array List Reference Question
Returning a value from a method is sufficient - no need for the break, except that you need to provide a default return value (the compiler does not know whether the return in the statement will actually be reached). Next, assuming deck is a List, if you want to find the index of an object in that List then use the indexOf method.
Similar Threads
-
enhanced 4 loop 4 2d array (mod. edit: enhanced for-loop for 2D array)
By java4amanda in forum New To JavaReplies: 5Last Post: 03-29-2012, 06:22 PM -
Using enhanced for loop to print array list??
By bdl1127 in forum New To JavaReplies: 1Last Post: 02-19-2012, 12:43 AM -
Setting Array List to label within for loop
By ajs1351 in forum New To JavaReplies: 5Last Post: 11-19-2011, 07:10 PM -
enhanced for loop
By billq in forum New To JavaReplies: 1Last Post: 05-01-2010, 02:07 PM -
Enhanced for loop
By Java Tip in forum Java TipReplies: 0Last Post: 11-03-2007, 09:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks