Results 1 to 3 of 3

Thread: help

  1. #1
    nishant is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default help

    here is my code ...could u please solve this issue. i have a problem while using the deck.length.



    public static void cardList(int numHands,int cardsPerHand){
    List<String> deck,hand;
    //52-card deck

    String[]suit=new String[]{"S","H","D","C"};
    String[] rank=new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q", "K"};

    deck=new ArrayList<String>();
    for(int i=0;i<deck.length; i++){
    for(int j=i;j<deck.length;j++){
    deck.add(rank[i]+"-"+suit[j]);
    }
    }

    Collections.shuffle(deck);//shuffles the heap of the crads

    //dealing the cards
    for(int i=0;i<numHands;i++){
    hand=deck.subList(deck.size()-cardsPerHand,deck.size());
    System.out.println(hand);
    //hand.clear();//removes the sub-list from the list
    }}}

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Read the API docs for ArrayList and tell me whether or not there is a length field in that Class, and what the method is that you should use instead.

    ArrayList (Java Platform SE 6)

    Edit: Besides, shouldn't you be using suit and rank instead? At that point, since you've just created it, the ArrayList is empty.

  3. #3
    satu is offline Member
    Join Date
    Aug 2008
    Posts
    12
    Rep Power
    0

    Default

    replace your code with this....

    String[]suit=new String[]{"S","H","D","C"};
    String[] rank=new String[]{"A","2","3","4","5","6","7","8","9","10","J"," Q", "K"};

    deck=new ArrayList<String>();
    for(int i=0;i<rank.length; i++){
    for(int j=i;j<suit.length;j++){
    deck.add(rank[i]+"-"+suit[j]);
    }
    }

    before adding any element List is empty and not work for you here..

Posting Permissions

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