import java.util.*;
public class ListDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int numHands=4;
int cardsPerHand=52/4;
String[] suit=new String[]{ "黑桃 ", "红心 ", "方砖 ", "梅花 "};
String[] rank=new String[]{ "ace ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10 ", "jack ", "queen ", "king "};
List deck=new ArrayList();
for(int i=0;i <suit.length-1;i++)
for(int j=0;j <rank.length-1;j++)
deck.add(rank[i]+suit[j]);///第21行出有问题,是在着不出来了,
Collections.shuffle(deck);
for(int i=0;i <numHands;i++)
System.out.println(perHand(deck,cardsPerHand));
}
private static List perHand(List deck, int n) {
// TODO Auto-generated method stub
int deckSize=deck.size();
List handView=deck.subList(deckSize-n,deckSize);
List hand=new ArrayList(handView);
handView.clear();
return hand;
}
}
运行后提示:Exception in thread "main " java.lang.ArrayIndexOutOfBoundsException: 4
at ListDemo.main(ListDemo.java:21)
这个是什么问题呀
------解决方案--------------------
for(int i=0;i <suit.length-1;i++)
for(int j=0;j <rank.length-1;j++)
deck.add(rank[i]+suit[j]);///第21行出有问题,是在着不出来了,
i跟j,或者rank跟suit弄反了吧,是rank[j],suit[i]吧
------解决方案--------------------
deck.add(rank[i]+suit[j]);
↓
deck.add(suit[i]+rank[j]);
这种问题debug一下不就知道了吗?
------解决方案--------------------
deck.add(rank[i]+suit[j]);
↓
deck.add(suit[i]+rank[j]);
楼主确实没有看清楚哦。。。