This discussion is archived
2 Replies Latest reply: Mar 26, 2007 8:52 AM by 807599 RSS

From list to array... problem

807599 Newbie
Currently Being Moderated
Hi,

I though if you used the .ToArray() your list was literally turned into an array and hence you can send it to a method or function which was expecting an array as input?!

I want to accept my lists of hc(hole cards) and fc(flop cards) and then add them into one list, convert that to an array, and then process that according to what stage we are at in the game. However, using the code below I keep getting the error that the eval_5hand is expecting an array, and I thought I had transformed the list into an array - what am I missing?

Many thanks, Ron
public void eval_hand(List hc, List fc)
     {
          List<Card> allCards = new ArrayList<Card>();
          allCards.addAll(hc);
          allCards.addAll(fc);
          allCards.toArray(new Card[ allCards.size() ]);
          
          switch (allCards.size()) {
            case 5: 
                 eval_5hand( allCards );
              break;
            case 6: 
                 eval_6hand( allCards );
              break;
            case 7: 
                 eval_7hand( allCards );
              break;
            default: 
              System.out.println("Not enough cards!");
                 break;
          }
     }
     
     private int eval_5hand( Card[] hand )
     {
         int c1, c2, c3, c4, c5;

         c1 = hand[0].intValue();
                       ...
  • 1. Re: From list to array... problem
    807599 Newbie
    Currently Being Moderated
    The toArray methods returns an array, it doesn't turn your List into an array.
              List<Card> allCards = new ArrayList<Card>();
              allCards.addAll(hc);
              allCards.addAll(fc);
              Card[] allCardsArray = allCards.toArray(new Card[ allCards.size() ]);
    By the way, do you really need dedicated methods for evaluating hands with 5, 6, and 7 cards ? (And couldn't this(these) method(s) take a List as parameter insetad of an array?)
  • 2. Re: From list to array... problem
    807599 Newbie
    Currently Being Moderated
    Hi, thanks for the answer, I see my mistake :) Completely misread that one.

    Thanks again, Ron