Results 1 to 2 of 2
  1. #1
    smande3 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default 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:

    Java 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)) {
    now from here im thinking in the if conditional statement body i should put

    Java Code:
    int findCardIndex = deck.get(testCard);
    return findCardIndex;
    break search;
    not sure if thats correct or if im making correct use of "break", or if break is needed since i declare return.

    im very new to java this site has been great so far

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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

  1. Replies: 5
    Last Post: 03-29-2012, 06:22 PM
  2. Using enhanced for loop to print array list??
    By bdl1127 in forum New To Java
    Replies: 1
    Last Post: 02-19-2012, 12:43 AM
  3. Setting Array List to label within for loop
    By ajs1351 in forum New To Java
    Replies: 5
    Last Post: 11-19-2011, 07:10 PM
  4. enhanced for loop
    By billq in forum New To Java
    Replies: 1
    Last Post: 05-01-2010, 02:07 PM
  5. Enhanced for loop
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •