Results 1 to 3 of 3
Thread: help
- 09-12-2008, 04:23 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
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
}}}
- 09-12-2008, 07:49 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 09-23-2008, 01:14 PM #3
Member
- Join Date
- Aug 2008
- Posts
- 12
- Rep Power
- 0
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..


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks